Search code examples
c#asp.net-mvcunobtrusive-validation

displaying decimal values depending on decimal places in string.format


I'm using String.Format for displaying validation messages. I'm trying to achieve a scenario where, if decimal is there, show 12.34, else don't show any decimal points like, 12.

I tried to achieve it by using type as number. My string is,

Please enter value between {1:N} and {2:N}. // Displays 1.00 and 2.00
Please enter value between {1:N0} and {2:N0}. // Displays 1 and 2

What I should do to fix this? I need comma separation depending on culture. Using {1:G} will not provide that.


Solution

  • The 0 means a digit will always be shown. Use a # to show optional digits.

    {1:0.##}