Answered by:
How to convert an exponential number to non exponential number?

Is there a way to convert an exponential number (held in string format) to nonexponential format without losing any precision in the number? For example, I have a string variable containing "7.7583877127496407E6" 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.
Question
Answers


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,
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, 


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

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... 

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