如何知道双字串是否是往返安全的? [英] How to know if a double string is round-trip safe?
问题描述
我有一个double的文本表示形式,想知道将它来回翻倍并返回是安全的.如果我还想接受任何数字风格的输入,我怎么知道呢?或者我怎么知道用Double.Parse解析双字符串时是否丢失任何精度?或者,我如何ToString一个双精度以匹配与另一个双弦相同的格式?我认为对这些问题中的任何一个答案都是一种解决方案.
I have a text representation of a double and want to know if it's safe to round-trip it to double and back. How do I know this if I also want to accept any kind of number-style of the input? Or how do I know if any precision is lost when a double-string is parsed with Double.Parse? Or how do I ToString a double to match the same format as another double-string? An answer to any of these questions would be a solution I think.
推荐答案
使用R
格式说明符将double
转换为string
:
Use the R
format specifier to convert the double
to a string
:
myDouble.ToString("R")
请参见往返("R")格式说明符在MSDN上.
往返("R")格式说明符可确保将转换为字符串的数值重新解析为相同的数值.仅Single, Double 和BigInteger类型支持此格式.
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.
(重点是我的)
这篇关于如何知道双字串是否是往返安全的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!