Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format decimal as a percent with specific decimal places

Tags:

c#

.net

decimal

<%= Model.STPData.InitialRateSetting.HasValue ? Model.STPData.InitialRateSetting.Value.ToString() : "" %>

The Model.STPData.InitialRateSetting is a decimal. I want to format that as a percentage, and then round it to 5 decimal places. How would I do that?

like image 610
slandau Avatar asked Mar 03 '11 16:03

slandau


Video Answer


2 Answers

you can use

Model.STPData.InitialRateSetting.Value.ToString("P5");

assuming InitialRateSetting is a decimal

like image 78
Ta01 Avatar answered Oct 18 '22 23:10

Ta01


You could use ToString("p5"). This will take the number 0.051234567 and display it as "5.12346%". I'm not sure if it'll round that last place, to make sure the behavior or lack thereof is what you want.

like image 31
KeithS Avatar answered Oct 18 '22 23:10

KeithS