Home > DeveloperSection > Forums > How can I convert string to decimal with trailing zero(s)
F Scott Fitzgerald
F Scott Fitzgerald

Total Post:29

Points:203
Posted on    October-05-2013 7:38 AM

 C# C# 
Ratings:


 1 Reply(s)
 928  View(s)
Rate this:

Suppose that we have stringvalue=125.32600 when it convert to decimal value with this code

decimal d;
decimal.tryparse(stringvalue,out d)

d value is 125.326 how can I do this convert with final result 125.32600



Ida B Wells
Ida B Wells

Total Post:30

Points:210
Posted on    October-05-2013 8:58 AM

Your code works as written (as long as the decimal separator matches your culture):

decimal d;
decimal.TryParse("125.32600", NumberStyles.Number, CultureInfo.InvariantCulture, out d);
s = d.ToString(CultureInfo.InvariantCulture); // 125.32600

Decimal already remembers how many trailing zeros it has. This is caused by decimal representing numbers in non-normalized form, with an integer mantissa and an exponent representing the number of decimal digits. e.g. 125.32600 is represented as 12532600 * 10^-5


Don't want to miss updates? Please click the below button!

Follow MindStick