小编Mee*_*sse的帖子

C#中的C++ std :: setprecision

我通过移植一些传统的C++代码来获取C#,并希望保持输出相同.曾经是过去的东西

output << std::setprecision(10) << (double) value;
Run Code Online (Sandbox Code Playgroud)

我想现在会

output.Write("{0:F10}", value);
Run Code Online (Sandbox Code Playgroud)

但这并没有成功.特别是值> 1会得到更多的数字.一个常见的在线建议是Math.Round首先,但如果总长度为零,则会附加零< 10.

所以我把它放在一起:

    // std::setprecision is not exactly the same as ":F10", mirror original behavior
    static string setPrecision(double value) {
        string ret = value.ToString();

        // Don't just Substring(0, 11), we need to apply rounding,
        // and don't always do this, we don't want to append zeroes,
        // for 10 digits + period, with 0.. not counting for total
        if(ret.Length > digits + 1) …
Run Code Online (Sandbox Code Playgroud)

c#

9
推荐指数
1
解决办法
1345
查看次数

标签 统计

c# ×1