?
Solved

Reducing DataRow Value down to 2 decimal places

Posted on 2009-06-30
10
Medium Priority
?
2,780 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

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