Solved

Reducing DataRow Value down to 2 decimal places

Posted on 2009-06-30
10
2,502 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
Comment Utility
Change Convert.ToString(drProducts["UnitPrice"]) to

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

Expert Comment

by:guru_sami
Comment Utility
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
Comment Utility
Try changing Convert.ToString(drProducts["PricePaid"]) to
drProducts["PricePaid"].ToString("#.##")
0
 
LVL 22

Expert Comment

by:prairiedog
Comment Utility
opps, too slow.
0
 
LVL 7

Assisted Solution

by:meispisces
meispisces earned 100 total points
Comment Utility
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
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

 
LVL 7

Expert Comment

by:meispisces
Comment Utility
rito01, is your problem solved? Please comment

thanks
0
 
LVL 1

Author Comment

by:rito1
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks all. This cracked it.
0

Featured Post

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

Join & Write a Comment

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

771 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

12 Experts available now in Live!

Get 1:1 Help Now