Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataBinding Eval To 2 Decimal Place Doesn't Show 0

Platform: C# ASP.NET 3.5

I have a ListView which builds a Rate field which is decimal, if I simply have <% #Eval("Rate") %> it shows 4.5000 rather than 4.5 if I use <% #Eval("Rate","{0:#.##}") %> it shows 4.5 but doesn't display 0

any suggests on how to bind the decimal field but still show 0

Thanks

Lee

like image 200
monkeylee Avatar asked Jul 14 '09 07:07

monkeylee


People also ask

How do you get a float number up to 2 decimal places?

format("%. 2f", 1.23456); This will format the floating point number 1.23456 up-to 2 decimal places, because we have used two after decimal point in formatting instruction %.

How do you correct to 2 decimal places in Java?

Just use %. 2f as the format specifier. This will make the Java printf format a double to two decimal places.

What is use of Databinder eval in asp net?

Eval) function is used to bind data in controls inside GridView, DataList, Repeater, DetailsView, etc. and using string. Format multiple values can be set to a single control.


3 Answers

Using #.## in the format means it should hide 0. Use 0.00 instead:

<%# Eval("Rate", "{0:0.00}") %>

See these examples:

String.Format("{0:0.00}", 123.4567);   // "123.46"
String.Format("{0:0.00}", 123.4);      // "123.40"
String.Format("{0:0.00}", 123.0);      // "123.00"
String.Format("{0:0.##}", 123.4567);   // "123.46"
String.Format("{0:0.##}", 123.4);      // "123.4"
String.Format("{0:0.##}", 123.0);      // "123"
like image 149
Blixt Avatar answered Sep 24 '22 00:09

Blixt


Did you try this :

<% #Eval("Rate","{0:F2}") %>
like image 31
Canavar Avatar answered Sep 23 '22 00:09

Canavar


This would work, it also adds a group separator: <%# Eval("Rate", "{0:n2}")%>

like image 21
devloper Avatar answered Sep 22 '22 00:09

devloper