Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

.net

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

like image 231
M Raymaker Avatar asked Dec 06 '25 07:12

M Raymaker


1 Answers

price.ToString(@"#,0.00;-#,0.00;0\,-").Replace(".00", ",-");
like image 83
KarmaEDV Avatar answered Dec 07 '25 20:12

KarmaEDV



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!