0

Within a view I have the following code:

@foreach (var ce in Model.MyObjectCollection)
{
    <p>
        Target: @ce.Target.ToString("#.##")%
        <br/>
        Level: @ce.Level.ToString("#.##")%
    </p>
}

If either the value of Target or Level is 0.00, then MVC simply renders that out as an empty string. Debugging shows the value of each to be 0.00.

I need zero values to render as 0.00%.

Can anyone advise if I'm doing something wrong?

EvilDr
  • 8,943
  • 14
  • 73
  • 133
  • Check http://stackoverflow.com/questions/9001603/why-does-0-tostring-return-an-empty-string-instead-of-0-00-or-at-least-0 – tweray Sep 02 '14 at 15:48

2 Answers2

4

The # format specifier won't display zeros

Note that this specifier never displays a zero that is not a significant digit, even if zero is the only digit in the string. It will display zero only if it is a significant digit in the number that is being displayed.

You can use the 0 specifier instead

ToString("0.00")%
James
  • 80,725
  • 18
  • 167
  • 237
1

Use zeros instead of #:

@ce.Target.ToString("0.00")%
JLe
  • 2,835
  • 3
  • 17
  • 30