Solved

How to strip off and format decimal numbers in repeater databind?

Posted on 2009-05-07
7
1,744 Views
Last Modified: 2013-12-17
I use repeater to bind a query with a MSSQL DB table.

<asp:Label ID="balance" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "balance")%>'></asp:Label>

It displays 2344.000000000

How can I ?
1) strip off it to be 2344.00 (2 decimal places)
2) format it to be 2,344.00 (with , for each 000)

0
Comment
Question by:techques
  • 3
  • 3
7 Comments
 
LVL 12

Expert Comment

by:GuitarRich
ID: 24324134
Change the label to this:
<asp:Label ID="balance" runat="server" Text='<%# string.Format("{0:#,##0.00}", DataBinder.Eval(Container.DataItem, "balance")) %>'></asp:Label>
 
0
 

Author Comment

by:techques
ID: 24324545
Compilation Error

CS1501: No overload for method 'ToString' takes '3' arguments


<asp:Label ID="balance" runat="server" Text='<%# string.Format("{0:#,##0.00}"), DataBinder.Eval(Container.DataItem, "balance")%>'></asp:Label>

Open in new window

0
 
LVL 12

Expert Comment

by:GuitarRich
ID: 24324589
check your code - the one you have posted is different to mine - the Text section should be:
string.Format("{0:#,##0.00}", DataBinder.Eval(Container.DataItem, "balance"))
 
you have
 
string.Format("{0:#,##0.00}"), DataBinder.Eval(Container.DataItem, "balance")
 
Check where the brackets are

Open in new window

0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:techques
ID: 24325054
sorry, you are right. I missed the ) in the end.

May I ask

I have a label

<asp:Label ID="totalbalance" runat=server></asp:Label>

in c#

if (ds.Read())
{
this.totalbalance.Text = string.Format("{0:#,##0.00}",ds["totalbalance"].ToString());
}

But, it cannot be formatted. do you know why?
0
 
LVL 12

Accepted Solution

by:
GuitarRich earned 50 total points
ID: 24325109
its probably the ds["totalbalance"].ToString() bit. If totalbalance is null that will fail - change the line to
this.totalbalance.Text = string.Format("{0:#,##0.00}",ds["totalbalance"]);
and it should be ok.
 
0
 

Author Closing Comment

by:techques
ID: 31578915
excellent !!
0
 

Expert Comment

by:edeveloper1
ID: 39194801
I have tried EVERYTHING to format a decimal number in a repeater databind and nothing works.  I still get trailing zeros no matter what I do. Any ideas?

NEVERMIND - was looking at the wrong column!!
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question