locked
Decimal value to string missing last two decimal value.. RRS feed

  • Question

  • User377613556 posted

    hi, When I am trying to convert a decimal value to string last decimal value is missing. 

    This is the decimal value I am using "1630.0" when I am converting to string I am getting as "1630" ( .0 is missing )

     my code 

    double after1 = 1630.0;
    string[] _newvalu = after1.ToString();  // 1630
    _value =_newvalu;

    Wednesday, July 19, 2017 8:19 PM

All replies

  • User753101303 posted

    Hi,

    Numeric values never cares about non significant digits and using 0001630.000 in your source code would still assign the SAME value to after1.

    You can use after1.ToString("n1"); to force showing 1 digit when converting this numeric value to a string See https://docs.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings for details.  

    Wednesday, July 19, 2017 8:44 PM
  • User-707554951 posted

    Hi Ramakrishna,

    You also could use string.Format method instead:

                double after1 = 1630.0;
                string _newvalu = string.Format("{0:0.0}", after1);
                Response.Write(_newvalu);

    Best regards

    Cathy 

    Thursday, July 20, 2017 3:20 AM
  • User-1865945774 posted

    Hello try using this code.

    float after1 = 1630.0f;

            double value = Math.Round(after1, 2);

                            label1.Text = string.Format("{0:0.00}", value);

    Wednesday, July 26, 2017 4:54 AM