编程语言
首页 > 编程语言> > c# – 如何知道双字符串是否是往返安全的?

c# – 如何知道双字符串是否是往返安全的?

作者:互联网

我有一个双重文本表示,并想知道它是否可以安全地往返双重和返回.如果我还想接受任何类型的数字输入,我怎么知道这个?或者,当使用Double.Parse解析双字符串时,如何知道是否有任何精度丢失?或者我如何ToString一个double来匹配与另一个双字符串相同的格式?任何这些问题的答案都是我认为的解决方案.

解决方法:

使用R格式说明符将double转换为字符串:

myDouble.ToString("R")

请参阅MSDN上的The Round-trip (“R”) Format Specifier.

The round-trip (“R”) format specifier guarantees that a numeric value that is converted to a string will be parsed back into the same numeric value. This format is supported only for the Single, Double, and BigInteger types.

(强调我的)

标签:c,parsing,double,double-precision
来源: https://codeday.me/bug/20190630/1332053.html