假设我们有 stringvalue=125.32600
用这段代码转换成十进制值
decimal d;
decimal.tryparse(stringvalue,out d)
d值为125.326 我怎样才能将最终结果转换为 125.32600
最佳答案
你不能,因为 125.32600
等于 125.326
。但是,在这种情况下,我猜您想以特定格式打印出来,可以这样做:
Console.WriteLine(d.ToString("f5"));
阅读Standard Numeric Format Strings
更新
扩展方法:
public string Format(this decimal source, int precision)
{
if (precision < 0)
{
throw new ArgumentOutOfRangeException("Precision must be a non negative integer");
}
return source.ToString("f" + precision);
}
可以这样使用:
Console.WriteLine(d.Format(5));
关于c# - 如何将字符串转换为带尾随零的十进制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19196873/