How to convert string to decimal with 3 decimal places?

string num = 23.6;

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

decimal nn = 23.600 

Is there any method?

+4
source share
4 answers

I try my best.

First of all, yours string num = 23.6;will not even compile. You need to use double quotes with your strings likestring num = "23.6";

If you want to get this as decimal, you first need to analyze it with a help IFormatProviderthat .has NumberDecimalSeparatorhow InvariantCulture(if your CurrentCultureuses .already, you don't need to pass a second parameter);

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

23.6 decimal. 23.6, 23.60, 23.600 23.60000000000 , ? , , , 23.6M . , . . " " .NET.

? , 23.600. , "F" Format Specifier .

string str = nn.ToString("F3", CultureInfo.InvariantCulture); // 23.600
+6

.

1 1.0 1.0000 +000001.00.

23.6. var d = decimal.Parse("23.6")

, 23.6, 23.600, d.ToString("F3")

+3

, , num.ToString( "#######. ###" )

0

, . , 3 , , string.Format:

decimal nn= 23.5;
var formattedNumber = string.Format("{0,000}", nn);
-2
source

All Articles