Converting exponential number to decimal 1.11111117E+9 - trailing digits become zero

我只是一个虾纸丫 提交于 2019-12-12 21:20:23

问题


I'm trying to convert and exponential number 1.11111117E+9 which is actually a 10 digit number '1111111111'. When I'm trying to convert this exponential number using decimal.TryParse method it is making last 3 digits as zero and giving the number as '111111000'. This is happening with any 10 digit number.

   decimal amount;
   decimal.TryParse("1.11111117E+9", NumberStyles.Any, null, out amount);

This is weird but I'm not able to figure out what's the issue here, can anybody tell me what's wrong in this?

Edit: Sorry for the misleading question. As Henrik mentioned in his answer is exactly what I'm facing.

float f = 1111111111;
string s = f.ToString();
decimal amount;
decimal.TryParse(s, NumberStyles.Any, null, out amount);

This will always return 1111111000? How do I address this issue to get the correct value? Change it to Double or Decimal from float datatype is the solution or anything else?


回答1:


It's easy to round-trip a float value...

float f = 1111111111;
string s = f.ToString("r"); // r = roundtrip
float g = float.Parse(s);

Now f and g will be the same... but that doesn't mean that either value is exactly 1111111111... because that value can't be represented as a float. The nearest value exactly representable as a float is 1111111168... which is why you're getting the 7 at the end of the scientific representation.

Basically, you shouldn't be using float for this in the first place. From the docs for System.Single:

By default, a Single value contains only 7 decimal digits of precision, although a maximum of 9 digits is maintained internally.

So trying to store a 10 digit number and expecting it to be stored exactly is a fool's errand.

It's hard to say whether you should be using double or decimal - it depends on what value you're really trying to store. If it's a "natural" value like weight or height, I'd go for double. If it's a "human" value like a price, I'd use decimal.




回答2:


This

decimal amount;
decimal.TryParse("1.11111117E+9", NumberStyles.Any, 
    CultureInfo.InvariantCulture, out amount);

sets amount to 1111111170M, as expected.

Note the CultureInfo.InvariantCulture, so it does not depend on your local settings.

Update: I suspect your real code looks something like this:

float f = 1111111111;
string s = f.ToString();
decimal amount;
decimal.TryParse(s, NumberStyles.Any, null, out amount);

f is displayed in the debuger as 1.11111117e+9, s is 1.111111e+9 and amount 1111111000M. The reason for all this is the limited precision of float.




回答3:


Try this, It works better for me

 decimal dec = Decimal.Parse("1.11111117E+9", System.Globalization.NumberStyles.Any);



回答4:


You should parse double, not decimal. Scientific notation has no place with decimal.

Update: The exact integer value is 1111111170. Not 111111111 which is about 10 times less than what it should be.



来源:https://stackoverflow.com/questions/4723537/converting-exponential-number-to-decimal-1-11111117e9-trailing-digits-become

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!