4

I Have used following code to format decimals

return string.Format(CultureInfo.CreateSpecificCulture("nb-NO"), "{0:N3}", decVal);

If the decVal do not contain decimals I do not want to show decimal points but I want to show the figure with the correct formatting without zero s , How to perform this ?

user2771704
  • 5,994
  • 6
  • 37
  • 38
not 0x12
  • 19,360
  • 22
  • 67
  • 133
  • 4
    Can you please show an example for input and expected output? – Soner Gönül Jul 02 '14 at 07:54
  • Possible duplicate of http://stackoverflow.com/questions/6951335/using-string-format-to-show-decimal-upto-2-places-or-simple-integer – Uwe Keim Jul 02 '14 at 07:55
  • 1
    @UweKeim That question is actually a bit different (although the correct answer to this one is buried there as well). – Luaan Jul 02 '14 at 07:58
  • @SonerGönül 74,200 with decimals and without 56,000 <= this should display as 56 , Thank you – not 0x12 Jul 02 '14 at 08:00
  • 1
    @UweKeim Ah, ok, so with this comment it's obvious that you were entirely right on the duplicate question, even though the OP has written it wrong :) – Luaan Jul 02 '14 at 08:04

1 Answers1

9

You can use custom numeric format like:

return string.Format(CultureInfo.CreateSpecificCulture("nb-NO"), "{0:0.###}", decVal);

You may want to read about standard numeric formats and custom numeric formats

EDIT:

To handle the thousands separators you could use:

return string.Format(CultureInfo.CreateSpecificCulture("nb-NO"), "{0:#,0.###}", decVal);

But, to handle some specific cases you'd better to implement formats like it is described in this SO thread.

P.S.: Thanks @Luaan for the (0.###);

Community
  • 1
  • 1
Eugene Podskal
  • 10,270
  • 5
  • 31
  • 53
  • 2
    I'd use `0.###` instead. You usually don't want to hide a zero :) – Luaan Jul 02 '14 at 07:55
  • @Luaan I need to use the comma as the decimal seperator – not 0x12 Jul 02 '14 at 08:01
  • 1
    @Kirov `.` in a format specifier is the decimal separator (`,` being the thousands separator). Use the proper culture and it will be written as a comma on output. – Luaan Jul 02 '14 at 08:03