【问题标题】:How to set 7-digit precision after the floating point [duplicate]如何在浮点数后设置7位精度[重复]
【发布时间】:2016-07-14 21:53:26
【问题描述】:

计算梯形面积的例子

Input 5, 7, 12, Output 72.0000000
Input 8.5, 4.3, 2.7, Output 49.5000000
Input 100, 200, 300,    Output 45000.0000000
Input 5, 7, 12, Output 72.0000000
Input 0.222, 0.333, 0.555,  Output 0.1540125

我需要答案看起来像上面总是有 7 位数字。

【问题讨论】:

  • 我不明白你的输入和输出之间的关系。
  • 这里有很多重复项。请先搜索。

标签: c# precision


【解决方案1】:

如果您打算只显示一定数量的数字,您可能需要在显示之前对结果进行四舍五入。

double number = 1.23456789;
string output = Math.Round(number, 7, MidpointRounding.AwayFromZero).ToString("N7")

上面output的值将是1.2345679

【讨论】:

  • 这是我一直在寻找的,因为我不知道要计算的数字并且我不能使用 String.Format。谢谢大佬
  • @AnakinSkywalker 好的,如果是这样的话,标记为答案。
【解决方案2】:
string.Format("{0:0.0000000}", 34.5223423523542);
//  34.5223424

string.Format("{0:N7}", 34.5223423523542);
//  34.5223424

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-30
    • 2010-12-22
    • 2011-01-19
    • 1970-01-01
    • 2012-06-23
    相关资源
    最近更新 更多