M Raymaker
M Raymaker

Reputation: 1271

How to format double value as price without currency sign and with special case for no decimals

I want to string format a double value - a product price - with two decimals when the price is eg. 12.99, and with a ",-" when the price is 12.00. Is that possible using the ToString() extension in C# .NET?

I have tried

price.ToString(@"#,0.00;-#,0.00;0\,-");

and this gives me "12.99" just fine. But 12.00 shows as "12.00", and I would prefer it to be "12,-". I use groups in the above statement to separate positive, negative and zero numbers.

Can this be done without doing if/else logic in the code?

Cheers Jens

Upvotes: 3

Views: 391

Answers (1)

KarmaEDV
KarmaEDV

Reputation: 1691

price.ToString(@"#,0.00;-#,0.00;0\,-").Replace(".00", ",-");

Upvotes: 1

Related Questions