locked
double value "0.069999999999999993".tostring() gets converted to "0.07" RRS feed

  • Question

  • User35525891 posted

    Hi 

    I am trying to convert "0.069999999999999993" to string. But, the value is rounded to "0.07". I don't want the truncation to be happen. How to achieve it??

    Please help.

    Friday, October 14, 2016 3:45 PM

Answers

  • User-654786183 posted

    If you want the full digits that the double value holds internally, you need to pass the "G17" format specifier to the method like this

    double val = 0.069999999999999993;
    Console.WriteLine(val.ToString("G17"));

    Please refer MSDN document for the same

    I also created a fiddle for this example

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 14, 2016 3:58 PM

All replies

  • User-654786183 posted

    If you want the full digits that the double value holds internally, you need to pass the "G17" format specifier to the method like this

    double val = 0.069999999999999993;
    Console.WriteLine(val.ToString("G17"));

    Please refer MSDN document for the same

    I also created a fiddle for this example

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 14, 2016 3:58 PM
  • User-1672470423 posted

    Try below:

    double text = 0.069999999999999993;
    Console.WriteLine(text.ToString("R"));

    Friday, October 14, 2016 4:02 PM
  • User347430248 posted

    Hi Mani,

     protected void Button2_Click(object sender, EventArgs e)
            {
                double data = 0.69999999999999993;
                
                string str = data.ToString("G17");
    
    
                TextBox1.Text = str;
            }

    Reference:

    Double.ToString Method (String)

    Regards

    Deepak

    Monday, October 17, 2016 3:01 AM