Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Reducing DataRow Value down to 2 decimal places

Posted on 2009-06-30
10
Medium Priority
?
2,851 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 22

Expert Comment

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

Assisted Solution

by:meispisces
meispisces earned 400 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
 
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 800 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 800 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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.…
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.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

610 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