-2

I want to format an integer so that it appears with the 1000's separator (,)

My attempts so far have been:

String.Format("{0:#,###.##}", 1234.0);            // 1,234
String.Format("{0:#,###.##}", 1234.05);            // 1,234.05
String.Format("{0:#,###.##}", 1234);            // 1,234

I am Struggling to display the values as output as 1,234.0.. Could you please suggest me how to output the string as 1,234.0 ??

er_Yasar
  • 150
  • 10
  • Possible duplicate of [.NET String.Format() to add commas in thousands place for a number](http://stackoverflow.com/questions/105770/net-string-format-to-add-commas-in-thousands-place-for-a-number) – Draken Apr 19 '16 at 06:53
  • String.Format("{0:#,###.##}", 1234.0); but how to do this ?? can you explain?? – er_Yasar Apr 19 '16 at 06:55
  • 1
    Have you tried `String.Format("{0:n}", 1234.0);` as the other post suggests? – Draken Apr 19 '16 at 06:56
  • 1
    https://dotnetfiddle.net/3lZHJD – Zohar Peled Apr 19 '16 at 06:59
  • Try: "{0:#,###.0}" – mortb Apr 19 '16 at 07:00
  • 1
    Slight modification to your fiddle @ZoharPeled to demonstrate the differences with rounding and if more decimal places wanted: https://dotnetfiddle.net/yqEXhE – Draken Apr 19 '16 at 07:01
  • @Draken [I'll take your bet and raise again :-)](https://dotnetfiddle.net/yqEXhE) – Zohar Peled Apr 19 '16 at 07:03
  • @Draken, I don't want to end with default decimals, For example string.Format("{0:#,##0.00}",123) //Output as 123.00 – er_Yasar Apr 19 '16 at 07:04
  • As in you want all the decimals to always be printed @er_Yasar? – Draken Apr 19 '16 at 07:05
  • If so, not really possible with `String.format()`: http://stackoverflow.com/questions/7795161/c-sharp-how-to-string-format-decimal-with-unlimited-decimal-places – Draken Apr 19 '16 at 07:06
  • @Draken, Nope, if the values are For example : 123 ==> Output as 123 and 123.0 ==> output as 123.0... – er_Yasar Apr 19 '16 at 07:07
  • 1
    `#` means "display digit here if necessary, or not if 0". `0` means "display digit here". So replace some of those #'s with 0's. Try `#,##0.0#`. – Lasse V. Karlsen Apr 19 '16 at 07:14

1 Answers1

3

The way I understand your question:

  • You want a thousand separator every 3 digits on the integer side
  • You want 1 digit on the fractional side

Additionally, I would guess that

  • You want at least 1 digit on the integer side

The problem with the format strings you're using is that you're using # to specify digit positions. According to the documentation, this character means:

Replaces the "#" symbol with the corresponding digit if one is present; otherwise, no digit appears in the result string.

(my emphasis)

on the other hand, the 0 character:

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

(again, my emphasis)

So you should use some 0's instead of #'s.

Specifically, here is the format string I would use according to the 3 bulletpoints at the top of this answer:

#,##0.0

This means:

  • comma means "add a thousand separator"
  • The #,##0 thus means "set aside place for the integer part here, add the thousand separator if necessary, and only add digits after the first if necessary, but always add at least one digit, so that you don't get just .1 as a result"
  • .0 means "1 fractional digit"
    • Your .# mean "add the decimal point and the fractional digit if the fractional digit is other than 0, if it is 0, don't add either", so that's probably the main problem with your formatting strings

Here's a .NET Fiddle to try it with.

Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825