HOW CAN I CONVERT STRING TO DECIMAL WITH TRAILING ZERO(S)

F Scott Fitzgerald

Total Post:29

Points:203
Posted by  F Scott Fitzgerald
C# 
C#
 1346  View(s)
Ratings:
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

  1. Ida B Wells

    Post:30

    Points:210
    Re: How can I convert string to decimal with trailing zero(s)

    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

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!