string xyz = "23.659";
return string.Format("{0:F"+2+"}", xyz);
output = 23.66
当我使用string。格式的值得到四舍五入,但我需要23.65作为输出。如何防止四舍五入的字符串格式?
您可以手动四舍五入值,例如在Math的帮助下。圆:
// If you have string (not decimal or double) we'll have parse
string xyz = "23.659";
return double.TryParse(xyz, out var value)
? string.Format("{0:F2}", Math.Round(value, 2, MidpointRounding.ToZero))
: xyz;
如果xyz
是float
,double
,decimal
类型,这是更自然的代码不需要解析:
double xyz = 23.659;
return string.Format("{0:F2}", Math.Round(xyz, 2, MidpointRounding.ToZero));