[英]c# format double to n decimals exactly
我想在小号后不断显示精确的n个小数。
所以(for = 2)如果我有13.4589,应该显示13.49; 如果是1512.0000,则应显示1512.00,而不仅仅是1512
我尝试了这个:
double value; //does contain the value to dislplay
int numberOfDecimals; //does contain the number of decimals I want to have permanently displayed
///first try
int temp = Convert.ToInt32(Math.Pow(10, numberOfDecimals));
string result1 = ((Math.Truncate(value * temp) / temp).ToString();
///second try
string temp = "0.";
for(int i = 0; i<numberOfDecimals;i++)
{
temp = temp + "#";
}
string result2 = value.ToString(temp);
两者都不会导致所需的输出(每当出现带有“ .000”的内容时,不会显示komma之后的零)..我必须更改什么?
您可以使用F
格式说明符 :
string result1 = ((Math.Truncate(value * temp) / temp).ToString("F2");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.