Solved

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

Posted on 2009-05-07
7
1,749 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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

821 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