Decimal Tostring help

Please help guys,

I'm reading a decimal value from my database for display on my winforms textbox. I call a method with the returned decimal...

private void SetValue(decimal Value)

{

      string tmpVal = Value.ToString;

}

at this point, if the Value parameter passed is something like 100
tmpVal is set to 100.00

and if the Value is something like 12.09
tmpVal is set to 12.090000

I dont understand it, for some reason, it appends zeros when converting tostring, i place breakpoints and I can see the value passed to the method is correct and the tostring method is padding the value with zeros..




Answer this question

Decimal Tostring help

  • D. Roman

    Thanks Peter,

    But I think you might have misunderstood my Issue:

    The problem is, the values I pass to the method have varying number of digits after the decimal point.

    I need to set the decimal place of the numericupdown control that will display these values based on the number of decimal places I read from the db object.

    To do this, I basically convert the decimal to string, count the number of digits after the decimal point in this string and use that value to set the decimal places of the numericupdown control.

    problem is, when I convert the decimal value recieved to string as shown in the method, I get  2 zeros padded for numbers without any decimal place eg:

    52 becomes 52.00,
    100 becomes 100.00

    and values with decimal places get padded with 4 zeros eg:

    123.098 becomes 123.0980000
    97.010 becomes 97.0100000

    this padding skews the decimal place setting for the numericupdown control






     Peter Ritchie wrote:
    Got me stumped on the "F" and "C".  Are you changing Thread.CurrentThread.CurrentCulture.NumberFormat or Thread.CurrentThread.CurrentUICulture.NumberFormat at all

    Regardless, you could always fall back to Value.ToString("#.00"); but, it will round differently than the currency format (if that's what you want to display).


  • Nuno_L

    You realize that the number of decimal places may be different for each value   Your users would have a numericupdown control that operates differently from value to value.

    Having said that, you could try:

    Value.ToString("#.##########")
     


    That gives me values up to and not including the first zero in the mantissa; but, you've noticed different behaviour that I elsewhere.  So, if you get padded zeroes you may have to resort to

    Value.ToString("#.##########").TrimEnd('0')
     

    This means, of course, what you present to the user may be completely different to what they've chosen in the regional settings (e.g. the number of values after decimal points, thousands seperator, etc.).

  • AZ17

    Welcome to the world of floating-point (see).  If you want the string representation in a specific format you have to use the Decimal.ToString(string) override, passing a format specifier as the string parameter.  For example, if you want to display the decimal value as currency, using the user's current setting for currency format: string tmpVal = Value.ToString("C").  If you want to format as a fixed-point decimal, using the user's current setting for numbers: string tmpVal = Value.ToString("F")



  • Emmanuel MacAdjeh MENSAH

    Both the "C" and "F" format use the regional settings from the control panel.  What have you set for the Numbers and Currency format

  • Wil Burton

    Got me stumped on the "F" and "C".  Are you changing Thread.CurrentThread.CurrentCulture.NumberFormat or Thread.CurrentThread.CurrentUICulture.NumberFormat at all

    Regardless, you could always fall back to Value.ToString("#.00"); but, it will round differently than the currency format (if that's what you want to display).

  • tsalikivenu

    This is crazy,

    I use Value.ToString("F") and I still get the padded decimal point and zero's

    I changed it to Value.ToString("F0"), and now I looose the decimal portion for values with decimal portions....how do I correct this.

    Some of the values have decimal portions and some dont

     Peter Ritchie wrote:

    Welcome to the world of floating-point (see).  If you want the string representation in a specific format you have to use the Decimal.ToString(string) override, passing a format specifier as the string parameter.  For example, if you want to display the decimal value as currency, using the user's current setting for currency format: string tmpVal = Value.ToString("C").  If you want to format as a fixed-point decimal, using the user's current setting for numbers: string tmpVal = Value.ToString("F")



  • mustafa1974

    Thanks Peter

    But I changed my the "the number of digits after decimal" for both Currency and Numbers in my control panel and I still get the same result.

  • Gordon411

    Thanks a million Peter....U r an Angel

    Thanks a lot...that takes care of my problem

    The NumericUpDown by default was rounding up the decimal values and my users wanted to see the fractional part of thier values...this led me to the fix you just got me out of

    Thanks...really appreciate your help

  • Decimal Tostring help