locked
How to convert a string to an scientific notation in c# code RRS feed

  • Question

  • Hi All,

    An excel cell having the number "9149022338" in general format and I could see the number is changed to scientific notation  in excel cell "9.15+E09" when the number string length is greater than the cell length. I need to perform the same with my C# application to convert the number string to an scientific notation. The scientific format decimal value reduced based on the cell width.

    Can any one please help me out on this.

    Thanks,

    Sridhar.S


    Sridhar.S

    Wednesday, March 20, 2013 8:50 AM

Answers

  • I guess you will have to format the string when setting it.

    label1.Text = string.Format("{0:E2}", MyNumber); 
    • Marked as answer by Bob Shen Thursday, April 4, 2013 8:10 AM
    Wednesday, March 20, 2013 10:23 AM
  • HI

    Read this thread...

    Parsing from scientific notation


    Abdul Jabbar

    • Marked as answer by Bob Shen Thursday, April 4, 2013 8:10 AM
    Wednesday, March 20, 2013 10:12 AM
  • Hi,

    Please read the MSDN-documentation Custom Numeric Format Strings. In the section named The "E" and "e" Custom Specifiers, you'll find the details you need. In order to adapt the length of the formatted string to the available space, you will have to use Graphics.MeasureString() to get the current length of the rendered string. Then, you'll have to compare this length to the available space, and adapt the formatting if needed (increase/decrease the number of zeros in value.ToString("0.###E+000", CultureInfo.InvariantCulture);). If you are using a DataGridView, you will also have to set the cell's FormattedValue by using the DataGridView.CellFormatting event.

    Marcel


    • Edited by Marcel Roma Wednesday, March 20, 2013 10:42 AM Added documentation link
    • Marked as answer by Bob Shen Thursday, April 4, 2013 8:10 AM
    Wednesday, March 20, 2013 10:40 AM

All replies

  • Is this in WPF or WinForms? If it is WPF then you can use StringFormat to get the number displayed with scientific notation.

    <TextBlock Width="150" Height="30" Text="{Binding MyNumber,StringFormat=E2}"/>

    (where MyNumber is the property with the value 9149022338)
    Wednesday, March 20, 2013 9:09 AM
  • Hi,

    Thanks for your reply.

    I need to done this in WinForms application.

    Please do the needful for this.

    Thanks,

    Sridhar.S


    Sridhar.S

    Wednesday, March 20, 2013 9:32 AM
  • HI

    Read this thread...

    Parsing from scientific notation


    Abdul Jabbar

    • Marked as answer by Bob Shen Thursday, April 4, 2013 8:10 AM
    Wednesday, March 20, 2013 10:12 AM
  • I guess you will have to format the string when setting it.

    label1.Text = string.Format("{0:E2}", MyNumber); 
    • Marked as answer by Bob Shen Thursday, April 4, 2013 8:10 AM
    Wednesday, March 20, 2013 10:23 AM
  • Hi,

    Please read the MSDN-documentation Custom Numeric Format Strings. In the section named The "E" and "e" Custom Specifiers, you'll find the details you need. In order to adapt the length of the formatted string to the available space, you will have to use Graphics.MeasureString() to get the current length of the rendered string. Then, you'll have to compare this length to the available space, and adapt the formatting if needed (increase/decrease the number of zeros in value.ToString("0.###E+000", CultureInfo.InvariantCulture);). If you are using a DataGridView, you will also have to set the cell's FormattedValue by using the DataGridView.CellFormatting event.

    Marcel


    • Edited by Marcel Roma Wednesday, March 20, 2013 10:42 AM Added documentation link
    • Marked as answer by Bob Shen Thursday, April 4, 2013 8:10 AM
    Wednesday, March 20, 2013 10:40 AM
  • I think the following code works fine:

            public static void ToScientific(double a, ref double number, ref double pow)
            {
                if (a == 0) { number = 0; pow = 0; return; }
                double b = a > 0 ? a : -a;
                pow = (int)Math.Log10(b);
                double k = b / Math.Pow(10, pow);

                if (k < 1) { pow--; }
                if (k >= 10) { pow++; }
     
                number = a / Math.Pow(10, pow);
            }







    Wednesday, December 20, 2017 1:50 PM