将double类型解析为不是指数形式的字符串类型 [英] parsing double type to string type not in exponential form

查看:166
本文介绍了将double类型解析为不是指数形式的字符串类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我有这行代码:

string name =name;

string tailCtr =9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999998;

double tailNumber = double.Parse(tailCtr);

++ tailNumber;

tailCtr = tailNumber.ToString(F0);

name = name + tailCtr;



问题是当执行double.Parse(tailCtr)时,它会转换为double但是呈指数形式。

现在,当tailNumber增加,并且tailNumber被转换为字符串时,tailCtr没有得到确切的数字。

有点四舍五入。



如果在解析为double并再次返回到字符串时如何保留数字的精度?

Basically, I have this line of code:
string name = "name";
string tailCtr = "99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999998";
double tailNumber = double.Parse(tailCtr);
++tailNumber;
tailCtr = tailNumber.ToString("F0");
name = name + tailCtr;

The problem is when double.Parse(tailCtr) is executed, it is converted to double but in exponential form.
Now, when tailNumber is incremented, and tailNumber is converted to string, tailCtr doesn''t get the exact number.
It is somewhat rounded off.

How can I retain the number''s precision when parsing to double and back again to string?

推荐答案

整个想法是错误的:键入 double 作为任何其他数字类型,没有指数形式。 指数形式仅适用于表示数字的字符串,而不适用于数字本身。



如果您想知道如何表示浮点数,请参阅:学习标准IEEE 754: http://en.wikipedia.org/wiki/IEEE_floating_point [ ^ ]。



It看起来你基本上知道如何控制字符串形式的数字表示,但以防万一:

http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/0c899ak8.aspx [ ^ ]。



如果您认为当tailNumber增加时, tailNumber被转换为字符串,再想一想:它没有被转换。如今,初学者中最糟糕的相关趋势之一就是尝试使用代表数据而不是数据的字符串。尽量不要落在那里;这是一件非常糟糕的事情。只有在必须在屏幕上或其他地方表示数据时才需要将数据转换为字符串,并且仅使用数字类型进行所有计算,而不是单个转换为字符串,并且不进行四舍五入。



最后,对于确切数字,您需要了解浮点数不是实数,它们只是将实数建模到某些有限的精度。很明显,单个实数通常包含无限量的信息;要理解它,你需要学习数学中的实数理论。要了解相关信息,请参阅: http://en.wikipedia.org/wiki/Real_number [ ^ ]。







查看 double 的精度:http://en.wikipedia.org/wiki/Double-precision_floating-point_format [ ^ ]。



它大约是16位小数数字。那你期待什么?



-SA
The whole idea is wrong: the type double as any other numeric type, do not have "exponential form". The "exponential form" is only applicable to a string representing numbers, not the numbers themselves.

If you want to know how floating-point numbers are represented, please see: learn the standard IEEE 754: http://en.wikipedia.org/wiki/IEEE_floating_point[^].

It looks like you basically know how to control presentation of numbers in string form, but just in case:
http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx[^],
http://msdn.microsoft.com/en-us/library/0c899ak8.aspx[^].

If you think that "when tailNumber is incremented, and tailNumber is converted to string", think again: it is not converted. One of the worst related trends in the beginners these days is trying to work with strings representing data instead of data. Try not to fall there; this is a very bad thing. You need to convert data to string only when you have to represent it on screen or elsewhere and do all calculation only with numeric types, without a single conversion to a string, and without rounding off.

Finally, as to "exact number", you need to understand that floating-point numbers are not real numbers, they only models real numbers to certain limited accuracy. It is apparent that a single real number generally contains infinite amount of information; to understand it, you need to learn the theory of real number in mathematics. To get an idea, please see: http://en.wikipedia.org/wiki/Real_number[^].



Look at the precision of double: http://en.wikipedia.org/wiki/Double-precision_floating-point_format[^].

It''s approximately 16 decimal digits. What do you expect then?

—SA


如果你读http://floating-point-gui.de/ [ ^ ]您的问题的答案应该变得清晰。
If you read http://floating-point-gui.de/[^] the answer to your question should become clear.


这两个答案都会给你足够的想法 - 我正在添加一些基本希望它只是这一点。你在这里给出的数字是98位十进制数。系统以二进制格式存储所有内容所以为了存储它你理想情况下需要大约250位或32字节。



但是双倍只需要8个字节。

所以,如何可以在8字节中存储32字节的信息。

如果在SA提供的链接中可以看到浮点数的定义格式,则会发生这种情况。



64位(或8字节)

1位用于标志( +或 -

11位指数:可以有一个数字 - 2048

52位表示有效精度:可精确保持数字 52二进制数字 21个十进制数字粗略。



所以当你超过21位十进制数字时 - 你不能指望它是准确的。

他们的表示就像以下



123456789012345678901 * Pow(2,X) - X是指数



这样它可以代表一个非常大的数字 - 但精确度将是 Pow(2,X)



直到 21十进制数 X 的值将 ZERO 。但是,当你超越它将有一些积极的价值和 Pow(2,X)将是变化的单位。小于 +1 -1 的操作无法精确完成。
Both the answers will give you enough idea - I am adding some basics hoping it will simply this a bit. The number you gave here is somewhere 98 decimal-digits long. system stores everything in binary format. so to store this you would ideally need around 250 bits or 32 bytes.

A double however takes only 8 bytes.
So, how it is possible to store a 32 bytes of information in 8 Bytes.
It happens with a defined formatting for floating point number as you can see in the links provided by SA.

In 64 bits (or 8 bytes)
1 bit is for sign (+ or -)
11 bits for exponent : can have a number up to - 2048
52 bits for significand precision : can precisely keep numbers up to 52 binary digits or 21 decimal digits roughly.

So when you go more than 21 decimal digit - you can''t expect that to be precise.
their representation will be like following

123456789012345678901 * Pow(2, X) -- X is the exponent

this way it can represent a very large number - but precision will be in terms of Pow(2, X)

Till 21 decimal digit the value of X would be ZERO. But as you go beyond that will have some positive value and the Pow(2, X) will be unit of change. A operation less than that like +1 or -1 can not be done precisely.


这篇关于将double类型解析为不是指数形式的字符串类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆