user354625
user354625

Reputation: 557

How to change this property to show % value in front end using asp.net mvc

In my class I have this property:

public decimal Percentage
    {
        get;
        set;
    }

This value is displaying as a decimal. Something like this:

-0.0214444

I need to show it something like this:

-2.14444

How can I change to this format in my property?

Upvotes: 1

Views: 242

Answers (2)

Matt Sherman
Matt Sherman

Reputation: 8358

Just multiply it by 100.

<%= myObject.Percentage * 100 %>

You can round it to a smaller number of decimal places:

<%= Math.Round(myObject.Percentage * 100, 3) %>

Another way:

<%= string.Format("Percentage is {0:0.0%}", myObject.Percentage) %>

Upvotes: 2

Brandon
Brandon

Reputation: 70002

You can use the "P" format string to add the percent sign. You can create another property to wrap it or just get Percentage and then call ToString.

public decimal Percentage { get; set; }

public string FormattedPercentage 
{ 
    get { return Percentage.ToString("P"); }
}

Upvotes: 2

Related Questions