william007 william007 - 1 month ago 10
C# Question

Convert string to 2 decimal place

I have a string (confirm to be of decimal expression)
0.4351242134

I want to convert to a string with two decimal place
0.44

How should I do in C#?

Answer
var probablyDecimalString = "0.4351242134";
decimal value;
if (Decimal.TryParse(probablyDecimalString , out value))
    Console.WriteLine ( value.ToString("0.##") );
else
    Console.WriteLine ("not a Decimal");
Comments