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
开发者_JAVA技巧 {
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?
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) %>
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"); }
}
精彩评论