-1

Is there a way to format, lets say, this number 36.9308706956375 into 36,930.87 ? I tried all kind of pattern including the one below it doesn't seem to work.It return 36,93.

decimal dec = 36.9308706956375;
var num = String.Format(CultureInfo.GetCultureInfo("ro-RO"), "{0:#,##0.00}", dec);

Thanks

David
  • 208,112
  • 36
  • 198
  • 279
John
  • 17
  • 7
  • Use ToString with a format at following webpage : https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-numeric-format-strings – jdweng Apr 08 '20 at 20:57
  • I suggest you to take a look into: [link] (https://learn.microsoft.com/en-us/dotnet/api/system.string.format?view=netframework-4.8) – Isaí Hinojos Apr 08 '20 at 20:57
  • 1
    Just to be clear in 36,930.87 is comma a decimal place separator or is the period suppose to be the decimal place separator? In one case you need to multiple by 1000 but the other one would likely require some custom code to get it to format with thousandth place separators after the decimal. – juharr Apr 08 '20 at 21:05
  • Does this answer your question? [format a number with commas and decimals in C# (asp.net MVC3)](https://stackoverflow.com/questions/16035506/format-a-number-with-commas-and-decimals-in-c-sharp-asp-net-mvc3) – devlin carnate Apr 08 '20 at 21:06
  • Note that even if your culture uses commas as a decimal separator the format string for numeric values always uses period to represent the decimal separator (which will be replaced by whatever the culture defines as the separator). – juharr Apr 08 '20 at 21:14
  • I think you want the format `"#,##0.000'.'00"` That will give you five places after the decimal and will insert a period after 3 places. This is assuming that you want comma as a decimal separator (based on the culture you are using). Unfortunately you cannot tell it to use the thousandth separator after the decimal so you have to hard code that period in there. Note however that format will be easily confused with a format using period as the decimal separator. – juharr Apr 08 '20 at 21:23
  • Do you want a format that multiplies the value by `1000`? Methinks you'll need to do the multiplication on your own and format _that_ result. – HABO Apr 08 '20 at 22:01

1 Answers1

0

It doesn't make sense to format 36.9308706956375 with thousands operator so I had to multiply it by 1000

(36.9308706956375 * 1000).ToString("N2")

Or

String.Format("{0:N2}", 36.9308706956375 * 1000)

More numeric formats can be found here in the official docs.

Anton Kahwaji
  • 467
  • 4
  • 15
  • I think the OP might be using comma as a decimal place separator instead of period and then they want a thousand place separator every 3 digits after the decimal like 36,930.870.695.637.5 but it's not really clear. – juharr Apr 08 '20 at 21:06