本文介绍了强制字符串为2位小数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个显示一个双中继器项目。偶尔双似乎走出来3位小数这样1165.833。我尝试通过在的String.Format方法包装它迫使它到小数点后两位,但它仍然散发出来的一样:
i have a repeater item that displays a double. occasionally the double seems to be coming out with 3 decimal places like this 1165.833. im trying to force it to two decimal places by wrapping it in a string.format method but it still comes out the same:
<%# String.Format("{0:f2}",DataBinder.Eval(Container.DataItem, "pricerange").ToString())%>
任何想法,为什么?
any ideas why?
推荐答案
字符串
根本不落实 IFormattable
。要使用格式化,删除的ToString(),这样,你是不是在传递一个String
String
simply does not implement IFormattable
. To use the formatting, remove .ToString() so that you aren't passing in a String.
<%# String.Format("{0:f2}",DataBinder.Eval(Container.DataItem, "pricerange"))%>
要看到更明确,运行此代码:
To see this more explicitly, run this code:
Console.WriteLine(string.Format("{0:f2}", "123.888"));
Console.WriteLine(string.Format("{0:f2}", 123.888));
它输出
which outputs
123.888
123.89
这篇关于强制字符串为2位小数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!