Prajwal
Prajwal

Reputation: 4000

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.

Upvotes: 1

Views: 57

Answers (2)

Afnan Makhdoom
Afnan Makhdoom

Reputation: 654

Try using :G . for isntance: Please enter value between {1:G} and {2:G}. Or {1:0.##}

Upvotes: 1

Simmetric
Simmetric

Reputation: 1661

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

{1:0.##}

Upvotes: 0

Related Questions