Solved

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

Posted on 2009-05-07
7
1,733 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:techques
Comment Utility
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
Comment Utility
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
Comment Utility
excellent !!
0
 

Expert Comment

by:edeveloper1
Comment Utility
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now