none
How to convert an exponential number to non exponential number?

    Question

  • Is there a way to convert an exponential number (held in string format) to non-exponential format without losing any precision in the number? For example, I have a string variable containing "7.7583877127496407E-6" and I want to convert it to a string in decimal format. It seems that converting it using a double or decimal variable as an intermediate step to do the reformatting only preserves 15 significant digits.

     

    Please help me as the fix need to be done as early as possible.

    Friday, May 18, 2007 6:32 AM

Answers

  • This preserves all the decimal places when I try it:

    decimal dec = decimal.Parse("7.7583877127496407E-6", System.Globalization.NumberStyles.Any);
    Console.WriteLine(dec);

    Friday, May 18, 2007 9:30 AM
  • You could parse the string, look for the dot ('.') and 'E'. When you get E, you know that what follows is the exponent. Then, you can calculate where you have to move the dot or how many leading/trailing zeros you need to add.

    In your example, after E, there is -6, meaning that you need 0.00000758... Since dot is the second character, this indicates that only 5 (6 - 1) zeros need to be added in front of your number. Of course, 0. should also be added and the old dot removed, because the first digit is NOT zero!

    Hopefully, this will be enough for you to get started.

    cheers,
    Friday, May 18, 2007 6:45 AM

All replies

  • You could parse the string, look for the dot ('.') and 'E'. When you get E, you know that what follows is the exponent. Then, you can calculate where you have to move the dot or how many leading/trailing zeros you need to add.

    In your example, after E, there is -6, meaning that you need 0.00000758... Since dot is the second character, this indicates that only 5 (6 - 1) zeros need to be added in front of your number. Of course, 0. should also be added and the old dot removed, because the first digit is NOT zero!

    Hopefully, this will be enough for you to get started.

    cheers,
    Friday, May 18, 2007 6:45 AM
  • Thanks Gregor. But i just want to know is there any other easy way or any library math function already available to convert it. i thought of this fix but just felt it as a round about way.

    Okay , if there is no other option i have to go for this.

    Friday, May 18, 2007 7:01 AM
  • This preserves all the decimal places when I try it:

    decimal dec = decimal.Parse("7.7583877127496407E-6", System.Globalization.NumberStyles.Any);
    Console.WriteLine(dec);

    Friday, May 18, 2007 9:30 AM
  • Hi matthew,

     

    i used the code but i am not able to covert it exponent to number.

     

    error is

     

    Value was either too large or too small for a Decimal.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.OverflowException: Value was either too large or too small for a Decimal.

    Source Error:

    Line 217:
    Line 218:        Dim saq As Decimal
    Line 219:        saq = Decimal.Parse(salary, System.Globalization.NumberStyles.Any)
    Line 220:
    Line 221:        Dim sq As String

     

     

    Thanks & regards

    Ankit Singhal

     

    Friday, May 25, 2007 5:42 AM
  • Decimal numbers have less range than doubles.

    What is the actual value of 'salary' in the code you posted?

    If it is out of the range of a decimal, it must be a pretty strange salary...

    Plus - that code doesn't look much like C# to me - what's that "Dim" all about? Maybe it's Visual Basic, about which I know nothing...
    Friday, May 25, 2007 9:08 AM
  • Hi,

    we have some calculation and result need to display from Non Exponential("1234567890.1234567890") to Exponential (1.234567890E+10)format.

    Please any one help me to resolve this problem?

    Regards,
    Jeyavel N
    Tuesday, November 17, 2009 9:26 AM
  • Only the decimal type can represent that many digits. The decimal type is very slow (hundreds of time slower than double) - is that ok for your app?

    Your example is wrong by the way - it should be E+09, not E+10.

    Try this:

    decimal value = 1234567890.1234567890m;
    Console.WriteLine(value.ToString("f10"));
    Console.WriteLine(value.ToString("e9"));
    • Proposed as answer by Louis.fr Tuesday, November 17, 2009 11:21 AM
    Tuesday, November 17, 2009 10:07 AM