我目前正在显示一个被舍入到3个小数位的数字,例如0.31,使用Math.Pow,唯一的问题是我想显示这个数字说0.310(为造型目的)有谁知道这是否可能?
答案 0 :(得分:2)
string.Format
可用于调用double x = 1493.1987;
string s1 = x.ToString("F3");
string s2 = string.Format("Your total is {0:F3}, have a nice day.", x);
// s1 is "1493.199"
// s2 is "Your total is 1493.199, have a nice day."
或double y = 1493;
string s3 = y.ToString("F3");
// s3 is "1493.000"
:
search_str
请注意,定点格式说明符始终显示您指定的小数位数。例如:
start_index = my_list.index(next(e for e in my_list if search_str in e))
end_index = my_list.index(next(e for e in my_list[start_index + 1:] if search_str in e))
答案 1 :(得分:0)
使用toString
double pi = 3.1415927;
string output = pi.ToString("#.000");
答案 2 :(得分:0)
这是一个更新的示例,该示例在无需调用.ToString()的情况下也有效:
float a = 12.3578f;
double b = 12.3578d;
Console.WriteLine("The tolerance specs are: {0:F4} and: {1:F3}", a,b);
答案:公差规格为:12.3578和:12.358