繁体   English   中英

如何在小数点C#之后始终显示3个数字

[英]How to always show 3 numbers after decimal point C#

我当前正在使用Math.Pow显示一个四舍五入到小数点后3位(例如0.31)的数字,唯一的问题是我想将此数字显示为0.310(出于样式目的),有人知道这是否可行吗?

定点格式说明符可用于ToStringstring.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"

使用toString的格式

double pi = 3.1415927;
string output = pi.ToString("#.000");

这是一个更新的示例,该示例也无需调用.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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM