none
Problem in truncating the characters after decimal point in a double variable RRS feed

  • Question

  • Hi,

    I am having a problem in truncating a variable which is double. I want my out put in double. I converted my

    double variable in string then truncated it to 16 characters after decimal using var.ToString("N16"). The

    output which i got is string now. When i again try to convert it to double, the output is correct but when i

    showed this output in messagebox. it is getting converted but it shows the value in Exponential term like

    2.77777777778E-05.

    I am not able to find the solution.

    output 0.000027777777777777779
    after showing it in message box it shows like this 2.77777777778E-05

    I want output as 0.0000277777777779 (16 characters).

    Please help me

    Thanks in advance

    Regards,
    Mona

    Tuesday, August 12, 2008 7:29 AM

Answers

  • Not sure what the problem is.  Try this code:

        private void button1_Click(object sender, EventArgs e) {
          double d = 0.000027777777777777779;
          MessageBox.Show(d.ToString());
          MessageBox.Show(d.ToString("N16"));
        }


    Hans Passant.
    • Marked as answer by Zhi-Xin Ye Friday, August 15, 2008 11:24 AM
    Friday, August 15, 2008 11:08 AM
    Moderator

All replies

  •  You supply this string "0.000027777777777777779" to the messagebox and somehow it gets converted to this string "2.77777777778E-05"?  What OS and Framework version are you using?
    Tuesday, August 12, 2008 10:38 AM
  • Not sure what the problem is.  Try this code:

        private void button1_Click(object sender, EventArgs e) {
          double d = 0.000027777777777777779;
          MessageBox.Show(d.ToString());
          MessageBox.Show(d.ToString("N16"));
        }


    Hans Passant.
    • Marked as answer by Zhi-Xin Ye Friday, August 15, 2008 11:24 AM
    Friday, August 15, 2008 11:08 AM
    Moderator