Solved

Reducing DataRow Value down to 2 decimal places

Posted on 2009-06-30
10
2,555 Views
Last Modified: 2012-05-07
Hi All,

I have the following FOREACH statement which loops through my DataTable.

An example value being produced by Convert.ToString(drProducts["PricePaid"]) is 10.000.

How would I apply formatting so that it only shows 2 decimal places.

Many thanks,

Rit
foreach (DataRow drProducts in dt.Rows)

                    {

                        strOrderTable = strOrderTable + "<tr>" +

                            "<td><strong>" + Convert.ToString(drProducts["Title"]) + "</strong><br />" +

                            @"<span class=""basket-subtext"">" + Convert.ToString(drProducts["Attributes"]) +

                            "Units: " + Convert.ToString(drProducts["Quantity"]) + " X " + Convert.ToString(drProducts["UnitPrice"]) +

                            "</span>" +

                            "</td>" +

                            "<td>" + Convert.ToString(drProducts["PricePaid"]) + "</td>" +

                            "<td>" + Convert.ToString(drProducts["VATPaid"]) + "</td>" +

                            "</tr>";

                    }

Open in new window

0
Comment
Question by:rito1
  • 3
  • 2
  • 2
  • +2
10 Comments
 
LVL 4

Expert Comment

by:rdoran0928
ID: 24746744
Change Convert.ToString(drProducts["UnitPrice"]) to

drProducts["UnitPrice"].ToString("0.00")
0
 
LVL 41

Expert Comment

by:guru_sami
ID: 24746750
try something like this:
drProducts["PricePaid"].ToString("N2");

e.g.
double d = 5.0000;
string s1 = d.ToString("N2");
Output:: s1="5.00"
0
 
LVL 22

Expert Comment

by:prairiedog
ID: 24746837
Try changing Convert.ToString(drProducts["PricePaid"]) to
drProducts["PricePaid"].ToString("#.##")
0
 
LVL 22

Expert Comment

by:prairiedog
ID: 24746885
opps, too slow.
0
 
LVL 7

Assisted Solution

by:meispisces
meispisces earned 100 total points
ID: 24747898
its quite easy...

You could use eiher Math.Round Function or Number.Format Function

Math.Round function does rounding upto x decimal places

Syntax : Math.Round ( expression, x)

Example : Math.Round( 5434.2323, 2)  = 5434.23

For  more details please refer : http://forums.asp.net/t/1104440.aspx

Number.Format Function is more general funciton and hence displays number in any format

Syntax (No such specific syntax it has) :

Refer http://msdn.microsoft.com/en-us/library/4fb56f4y.aspx

I don't remember much about Number.Format Function. I need to refer my  notes for them, if you could wait, give me 12 hours :-(, i would give you its solution. Til  then use Math.Round

Hope that helps you!

thanks

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 7

Expert Comment

by:meispisces
ID: 24752117
rito01, is your problem solved? Please comment

thanks
0
 
LVL 1

Author Comment

by:rito1
ID: 24752303
Hi All,

By applying

drProducts["PricePaid"].ToString("#.##")
or
drProducts["PricePaid"].ToString("N2");
or
drProducts["UnitPrice"].ToString("0.00")

I get the error:

No overload for method 'ToString' takes '1' arguments


Applying...

Math.Round(drProducts["VATPaid"],2)

I get the following error:

The best overloaded method match for 'System.Math.Round(double, int)' has some invalid arguments
and
Argument '1': cannot convert from 'object' to 'double'

Thanks,

RIt


0
 
LVL 22

Accepted Solution

by:
prairiedog earned 200 total points
ID: 24754752
C# is very strict. Try this:
First convert dr["PricePaid"] to a numeric type: Convert.ToDecimal()
Then use ToString("#.##"), or ToString("0.00"), or ToString("N2") to get the result
(Convert.ToDecimal(dr["PricePaid"])).ToString("#.##")
0
 
LVL 41

Assisted Solution

by:guru_sami
guru_sami earned 200 total points
ID: 24754778
I see because drProducts["PricePaid"] is object type..

So try this:
Convert.ToDouble(drProducts["PricePaid"]).ToString("N2");

Convert.ToDouble first and then use any of those methods ...should work...
0
 
LVL 1

Author Closing Comment

by:rito1
ID: 31598388
Thanks all. This cracked it.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

920 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

16 Experts available now in Live!

Get 1:1 Help Now