Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String formatting for decimal places and thousands

Tags:

c#

.net

I would like to represent the number 2.3421 as 2.34 but my current formatting shows it as 02.34

If I had the number 1342.323 I would want to show this as 1,342.32

If I had 0.23 this would be shown as 0.23.

What do I change my format string to achieve this? I have:

"{0:0,0.00}"

like image 734
mezamorphic Avatar asked Mar 04 '26 13:03

mezamorphic


2 Answers

Use # where a number is optional instead of 0:

"{0:#,0.00}"

See Custom Numeric Format Strings on MSDN:

"#" | Digit placeholder

Replaces the pound sign with the corresponding digit if one is present; otherwise, no digit appears in the result string.

like image 158
Oded Avatar answered Mar 06 '26 02:03

Oded


Try this:

{0:#,##0.00}

1342.323 should then be 1,342.32

like image 29
Andreas Rohde Avatar answered Mar 06 '26 02:03

Andreas Rohde



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!