C#| 使用String.Format()方法将小数点后的数字四舍五入
为了将小数点后的数字四舍五入,我们可以使用String.Format()方法,这是示例。
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
//只有两个小数点
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();
}
}
}输出结果
Two digits after decimal point 512.42 512.40 512.00 Three digits after decimal point 512.425 512.400 512.000
热门推荐
10 诗词送行祝福语大全简短
11 新房开工吉日祝福语简短
12 50多岁生日简短祝福语
13 安徽疫情祝福语简短英语
14 农民朋友发财祝福语简短
15 对生活祝福语简短精辟
16 搬家词简短祝福语朋友
17 女神结婚快乐祝福语简短
18 文学短句祝福语大全简短