在c#中将科学记数转换为实数 [英] Convert scientific notation number to real number in c#
问题描述
十进制金额;
decimal.TryParse(ds.Tables [0] .Rows [row] [col] .ToString(),NumberStyles.Any,CultureInfo.InvariantCulture,out amount);
我使用上面的代码将科学记数转换为实数,
它将1.09E + 15转换为1090000000000000
但是当我尝试转换1.09E + 30时它转换为0.
请告诉我如何转换这个更大的数字。
System.Decimal不能表示1.09E + 30之类的值。见这里:
十进制结构 [ ^ ]
十进制值的二进制表示由1位符号,96位整数和用于除以96位整数的比例因子组成,并指定它的哪个部分是小数分数。缩放因子隐式为数字10,提升到0到28之间的指数。因此,十进制值的二进制表示形式为((-2 ^ 96到2 ^ 96)/ 10 ^(0到28) ),其中 - (2 ^ 96-1)等于MinValue,2 ^ 96-1等于MaxValue。
你必须使用System.Double相反。
double 金额;
double .TryParse(ds.Tables [ 0 ]。行[row] [col] .ToString(),NumberStyles.Any,CultureInfo.InvariantCulture, out 金额);
如果你想以不同的方式呈现价值,你应该写:
string stringRepresentationOfDoubleValue = amount.ToString( #,## 0 ); // 或者您需要的任何格式字符串
关于数字格式字符串,请参考:
自定义数字格式字符串 [ ^ ]
[/ Edit]
Decimal [ ^ ]不能容纳那么大的数字。试试 BigInteger [ ^ ]。
[更新]
假设浮点数不适合你,你应该在那里寻找自定义库,比如 BigNumber [ ^ ],或GNU BigNum [ ^ ]。
试试这个,如果有任何问题让我知道..
ToString(doubleVar,System.Globalization.NumberStyles.Number )
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.
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.
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:
string stringRepresentationOfDoubleValue = amount.ToString("#,##0"); // Or whatever format string you need here
Regarding number format strings, please refer to:
Custom Numeric Format Strings[^]
[/Edit]
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[^].
try this, if any issue let me know..
ToString(doubleVar, System.Globalization.NumberStyles.Number)
这篇关于在c#中将科学记数转换为实数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!