将 Double 转换为字符串(完整数字)C#

Posted

技术标签:

【中文标题】将 Double 转换为字符串(完整数字)C#【英文标题】:Convert Double to String (The complete number) C# 【发布时间】:2018-09-12 17:16:48 【问题描述】:

我想做一个 DoubleString 的转换,但是有一个问题。 如果我使用以下代码将 Double 转换为 String:

static void Main(string[] args)

    double dou = 1000000000000000000;
    // Result dou = 1E+18
    string str = dou.ToString();
    // Again Result str = 1E+18

我需要结果是str = 1000000000000000000

我该怎么做?

【问题讨论】:

这可能是 C# 吗?请明确说明这应该是什么编程语言! 看起来像 Double to string conversion without scientific notation 的副本,同意吗? 【参考方案1】:

这应该可行

decimal dec = (decimal)dou;
string str = dec.ToString();

【讨论】:

【参考方案2】:

是 C# 吗?

https://docs.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings#the-numeric-n-format-specifier 尝试玩:

dou.ToString("N")

【讨论】:

此结果 = 1,000,000,000,000,000,000.00。我只需要 结果 = 1000000000000000000

以上是关于将 Double 转换为字符串(完整数字)C#的主要内容,如果未能解决你的问题,请参考以下文章