6
string num = 23.6;

I want to know how can I convert it into decimal with 3 decimal places like

decimal nn = 23.600 

Is there any method?

Hamid Pourjam
  • 20,441
  • 9
  • 58
  • 74
Bhavin Bhaskaran
  • 572
  • 5
  • 17
  • 41

4 Answers4

7

I try my best..

First of all your string num = 23.6; won't even compile. You need to use double quotes with your strings like string num = "23.6";

If you wanna get this as a decimal, you need to parse it first with a IFormatProvider that have . as a NumberDecimalSeparator like InvariantCulture(if your CurrentCulture uses . already, you don't have to pass second paramter);

decimal nn = decimal.Parse(num, CultureInfo.InvariantCulture);

Now we have a 23.6 as a decimal value. But as a value, 23.6, 23.60, 23.600 and 23.60000000000 are totally same, right? No matter which one you parse it to decimal, you will get the same value as a 23.6M in debugger. Looks like these are not true. See Jon Skeet comments on this answer and his "Keeping zeroes" section on Decimal floating point in .NET article.

Now what? Yes, we need to get it's textual representation as 23.600. Since we need only decimal separator in a textual representation, The "F" Format Specifier will fits out needs.

string str = nn.ToString("F3", CultureInfo.InvariantCulture); // 23.600
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
  • 1
    (I know this is two years later...) "But as a value, 23.6, 23.60, 23.600 and 23.60000000000 are totally same, right?" No, they're not. `decimal` retains trailing zeroes. `Console.WriteLine(decimal.Parse("1.0"))` and `Console.WriteLine(decimal.Parse("1.00"))` do different things. – Jon Skeet Oct 05 '17 at 07:14
  • @JonSkeet Hmm, if I understand correctly (which I don't think so but..), as a "value", they are same. `decimal.Parse("1.0") == decimal.Parse("1.000000")` returns `true` _no matter_ how many trailing zeros they have. But, what do you mean "do different things" in your sentence exactly? Is it the issue with "Keeping zeroes" on your [article](http://csharpindepth.com/Articles/General/Decimal.aspx)? – Soner Gönül Oct 05 '17 at 07:28
  • 1
    @SonerGönül: For "do different things", did you try running the code? They print out "1.0" and "1.00" respectively. Yes, the `==` operator ignore the scale, but saying multiple values are "totally same" is incorrect - there is an easily-detectable difference between those values, in that you just have to convert them to strings. And yes, this is the first part of the "Keeping zeroes" section of that article. – Jon Skeet Oct 05 '17 at 07:50
  • 1
    And likewise "No matter which one you parse it to decimal, you will get the same value as a 23.6M in debugger" is incorrect - if you parse "23.6", you get 23.6m. If you parse "23.60" you get 23.60m etc. The debugger will show you the difference between those. – Jon Skeet Oct 05 '17 at 07:51
5

There are two different concepts here.

  1. Value
  2. View

you can have a value of 1 and view it like 1.0 or 1.0000 or +000001.00.

you have string 23.6. you can convert it to decimal using var d = decimal.Parse("23.6")

now you have a value equals to 23.6 you can view it like 23.600 by using d.ToString("F3")

you can read more about formatting decimal values

Hamid Pourjam
  • 20,441
  • 9
  • 58
  • 74
  • 2
    But decimal stores the original number of decimal places which you can see if you use `nn.ToString()`. It does not show `23.6` but `23.600`. [MSDN](https://msdn.microsoft.com/en-us/library/System.Decimal(v=vs.110).aspx): _"The scaling factor also preserves any trailing zeros in a Decimal number. Trailing zeros do not affect the value of a Decimal number in arithmetic or comparison operations. However, trailing zeros might be revealed by the ToString method if an appropriate format string is applied."_ – Tim Schmelter Jun 10 '15 at 11:06
0

the thing that works for me in my case is num.ToString("#######.###")

M. Ali
  • 41
  • 2
-2

A decimal is not a string, it does not display the trailing zeros. If you want a string that displays your 3 decimal places including trailing zeros, you can use string.Format:

decimal nn= 23.5;
var formattedNumber = string.Format("{0,000}", nn);
LInsoDeTeh
  • 150
  • 2
  • `decimal` *does* display trailing zeroes. For example: `Console.WriteLine(1.000m)` and `Console.WriteLine(1.0m)` print 1.000 and 1.0 respectively. – Jon Skeet Oct 05 '17 at 07:15