将字符串强制到小数点后两位

时间:2021-03-25 16:36:21

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:

我有一个中继项目,显示一个双。有时双数似乎会出现3位小数,比如1165.833。我试着用字符串把它包起来,把它压缩到小数点后两位。格式方法,但结果还是一样的:

<%# String.Format("{0:f2}",DataBinder.Eval(Container.DataItem, "pricerange").ToString())%>

any ideas why?

任何想法为什么?

5 个解决方案

#1


59  

String simply does not implement IFormattable. To use the formatting, remove .ToString() so that you aren't passing in a String.

字符串不实现IFormattable。要使用格式,请删除. tostring(),以免传入字符串。

<%# 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


14  

You can use:

您可以使用:

String.Format("{0:0.00}",value);

#3


7  

Based on MSDN, you should be able to express the format mask within the call to DataBinder.Eval.
http://msdn.microsoft.com/en-us/library/2d76z3ck%28VS.90%29.aspx

在MSDN的基础上,您应该能够在对DataBinder.Eval的调用中表示格式掩码。http://msdn.microsoft.com/en-us/library/2d76z3ck%28VS.90%29.aspx

So essentially you should be able to do this - and force only 2 decimal places to show:

所以本质上,你应该可以这样做,只需要用两个小数来表示:

<%# DataBinder.Eval(Container.DataItem, "pricerange", "{0:##0.00}")%>

#4


3  

Try not calling ToString() on the output of the Eval method - you can't format a string with number formatting strings.

尝试在Eval方法的输出上不调用ToString()—您不能用数字格式化字符串来格式化字符串。

#5


0  

simple: DataBinder.Eval(Container.DataItem, "pricerange").ToString("C2")

简单:DataBinder.Eval(容器。DataItem pricerange).ToString(C2)

more @ http://msdn.microsoft.com/pt-br/library/dwhawy9k(v=vs.110).aspx#CFormatString

更多@ http://msdn.microsoft.com/pt-br/library/dwhawy9k(v = vs.110). aspx # CFormatString

#1


59  

String simply does not implement IFormattable. To use the formatting, remove .ToString() so that you aren't passing in a String.

字符串不实现IFormattable。要使用格式,请删除. tostring(),以免传入字符串。

<%# 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


14  

You can use:

您可以使用:

String.Format("{0:0.00}",value);

#3


7  

Based on MSDN, you should be able to express the format mask within the call to DataBinder.Eval.
http://msdn.microsoft.com/en-us/library/2d76z3ck%28VS.90%29.aspx

在MSDN的基础上,您应该能够在对DataBinder.Eval的调用中表示格式掩码。http://msdn.microsoft.com/en-us/library/2d76z3ck%28VS.90%29.aspx

So essentially you should be able to do this - and force only 2 decimal places to show:

所以本质上,你应该可以这样做,只需要用两个小数来表示:

<%# DataBinder.Eval(Container.DataItem, "pricerange", "{0:##0.00}")%>

#4


3  

Try not calling ToString() on the output of the Eval method - you can't format a string with number formatting strings.

尝试在Eval方法的输出上不调用ToString()—您不能用数字格式化字符串来格式化字符串。

#5


0  

simple: DataBinder.Eval(Container.DataItem, "pricerange").ToString("C2")

简单:DataBinder.Eval(容器。DataItem pricerange).ToString(C2)

more @ http://msdn.microsoft.com/pt-br/library/dwhawy9k(v=vs.110).aspx#CFormatString

更多@ http://msdn.microsoft.com/pt-br/library/dwhawy9k(v = vs.110). aspx # CFormatString