Solved

Reducing DataRow Value down to 2 decimal places

Posted on 2009-06-30
10
2,639 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
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!

 
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
 
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

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

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
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.
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

730 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