To round the digits after the decimal point, we can use String.Format() method, here is the example.
为了将小数点后的数字四舍五入,我们可以使用String.Format()方法,这里是示例。
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
//Only two decimal points
Console.WriteLine("Two digits after decimal point");
Console.WriteLine(String.Format("{0:0.00}", 512.4246));
Console.WriteLine(String.Format("{0:0.00}", 512.4));
Console.WriteLine(String.Format("{0:0.00}", 512.0));
Console.WriteLine("\n\nThree digits after decimal point");
Console.WriteLine(String.Format("{0:0.000}", 512.4246));
Console.WriteLine(String.Format("{0:0.000}", 512.4));
Console.WriteLine(String.Format("{0:0.000}", 512.0));
Console.WriteLine();
}
}
}
Output
输出量
Two digits after decimal point
512.42
512.40
512.00
Three digits after decimal point
512.425
512.400
512.000
翻译自: https://www.includehelp.com/dot-net/rounding-digits-after-decimal-point-using-string-format-method.aspx