Click here to Skip to main content
16,003,474 members
Please Sign up or sign in to vote.
2.80/5 (3 votes)
See more:
decimal amount;
decimal.TryParse(ds.Tables[0].Rows[row][col].ToString(), NumberStyles.Any, CultureInfo.InvariantCulture, out amount);

I used above code to convert scientific notation number to real number,
It converts 1.09E+15 as 1090000000000000
but when i try to convert 1.09E+30 it converted to 0.

Please tell me how to convert this larger number.
Posted
Updated 25-Jul-17 0:24am

System.Decimal cannot represent a value such as 1.09E+30. See here:
Decimal Structure[^]
The binary representation of a Decimal value consists of a 1-bit sign, a 96-bit integer number, and a scaling factor used to divide the 96-bit integer and specify what portion of it is a decimal fraction. The scaling factor is implicitly the number 10, raised to an exponent ranging from 0 to 28. Therefore, the binary representation of a Decimal value the form, ((-2^96 to 2^96) / 10^(0 to 28)), where -(2^96-1) is equal to MinValue, and 2^96-1 is equal to MaxValue.

You have to use System.Double instead.
C#
double amount;
double.TryParse(ds.Tables[0].Rows[row][col].ToString(), NumberStyles.Any, CultureInfo.InvariantCulture, out amount);


[Edit]
If you want to present the value differently, you should write:
C#
string stringRepresentationOfDoubleValue = amount.ToString("#,##0"); // Or whatever format string you need here

Regarding number format strings, please refer to:
Custom Numeric Format Strings[^]
[/Edit]
 
Share this answer
 
v2
Comments
kalaivanan from Bangalore, India 18-Apr-14 9:22am    
I tried double.TryParse(ds.Tables[0].Rows[row][col].ToString(), NumberStyles.Any, CultureInfo.InvariantCulture, out amount);
it converted 1.09E+30 to 1.09E+30
I tried
double amount = double.Parse(ds.Tables[0].Rows[row][col].ToString(), NumberStyles.Float, CultureInfo.InvariantCulture).ToString();
It converted to 0.
Any other data type is there,please tell me
phil.o 18-Apr-14 9:42am    
it converted 1.09E+30 to 1.09E+30 => ??
kalaivanan from Bangalore, India 18-Apr-14 23:53pm    
When i try to convert this value 1.09E+30 it returns the same value
phil.o 19-Apr-14 11:42am    
There is something important here you seem to miss: when you read 1.09E+30, that does not mean that the computer is storing this as a string value; what you see is the representation of the real value that the computer actually stores and manipulates. It just presents you this value under scientific notation, simply because 1.09E+30 is way much more readable than 1090000000000000000000000000000.
Do you get it? There is nothing to convert, because you confound the value with its human-readable representation.
See my updated solution.
Decimal[^] can not hold a number that large. Try BigInteger[^] instead.

[Update]
Supposing floating point numbers are not suitable for you, you should look for custom libraries out there, like BigNumber[^], or GNU BigNum[^].
 
Share this answer
 
v2
Comments
phil.o 18-Apr-14 9:12am    
BigInteger class holds integer values, not real ones :)
The good choice is the double type, here.
Zoltán Zörgő 18-Apr-14 9:19am    
Good catch. I was derouted by the examples given.
kalaivanan from Bangalore, India 18-Apr-14 9:16am    
BigInteger is available only in .NET Framework 4.5
,but am running in .NET Framework 3.5,any other data type is there to hold bigger value.
Zoltán Zörgő 18-Apr-14 9:21am    
Also true. Than I suppose you don't have any built-in type for such large fixed point numbers. Double is floating point, thus might not be suitable for you.
Sergey Alexandrovich Kryukov 18-Apr-14 11:22am    
5ed.
Also, OP does not understand that there is no such thing as "scientific notation number".
—SA
try this, if any issue let me know..
ToString(doubleVar, System.Globalization.NumberStyles.Number)
 
Share this answer
 
Comments
kalaivanan from Bangalore, India 18-Apr-14 23:53pm    
In .NET Framework 3.5, ToString() not accepting any paramaters
[no name] 19-Apr-14 12:04pm    
That is completely wrong. ToString can take no parameters, a string parameter, an IFormatProvider parameter or a string AND an IFormatProvider.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900