473,385 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Simple math is giving me trouble

I am trying to add dollar amounts together and add sales tax but everthing
after the decimal point is being cut off in the dollar amounts. Here is my
code:

if (Adults != "")
{
AdultTotal = int.Parse(Adults) * Convert.ToInt32(myReader["RateAdult"]);

ttlAdults.Text = Adults;
}

if (Children != "")
{
ChildTotal = int.Parse(Children) * Convert.ToInt32(myReader["RateChild"]);

ttlChildren.Text = Children;
}

double Tax = (AdultTotal + ChildTotal ) * (.06);
double TotalCost = (AdultTotal + ChildTotal );

txtTotal.Text = TotalCost.ToString() + " " + Tax.ToString();

The Adults and Children variables are ints and contain a quantity amount
from a form. The rate is grabbed from the database this is where the problem
is everything after the decimal in the rates are being chopped off.

I know I am probably using the wrong types. What is correct way to do this?

Thank you very much, Justin.
Nov 18 '05 #1
6 1372
I don't really understand your question... when you speak of rates you mean
the tax?
anyway, double is good for numerical simulation but bad for accounting
I would advice you to use the decimal type
"Justin" <Ju****@discussions.microsoft.com> wrote in message
news:73**********************************@microsof t.com...
I am trying to add dollar amounts together and add sales tax but everthing
after the decimal point is being cut off in the dollar amounts. Here is my
code:

if (Adults != "")
{
AdultTotal = int.Parse(Adults) * Convert.ToInt32(myReader["RateAdult"]);

ttlAdults.Text = Adults;
}

if (Children != "")
{
ChildTotal = int.Parse(Children) * Convert.ToInt32(myReader["RateChild"]);

ttlChildren.Text = Children;
}

double Tax = (AdultTotal + ChildTotal ) * (.06);
double TotalCost = (AdultTotal + ChildTotal );

txtTotal.Text = TotalCost.ToString() + " " + Tax.ToString();

The Adults and Children variables are ints and contain a quantity amount
from a form. The rate is grabbed from the database this is where the
problem
is everything after the decimal in the rates are being chopped off.

I know I am probably using the wrong types. What is correct way to do
this?

Thank you very much, Justin.

Nov 18 '05 #2
Int's don't carry any information beyond the decimal place. You could use
doubles or floats. Also you should add your Tax into the TotalCost to come
up with the GrandTotal.
"Justin" <Ju****@discussions.microsoft.com> wrote in message
news:73**********************************@microsof t.com...
I am trying to add dollar amounts together and add sales tax but everthing
after the decimal point is being cut off in the dollar amounts. Here is my
code:

if (Adults != "")
{
AdultTotal = int.Parse(Adults) * Convert.ToInt32(myReader["RateAdult"]);

ttlAdults.Text = Adults;
}

if (Children != "")
{
ChildTotal = int.Parse(Children) * Convert.ToInt32(myReader["RateChild"]);

ttlChildren.Text = Children;
}

double Tax = (AdultTotal + ChildTotal ) * (.06);
double TotalCost = (AdultTotal + ChildTotal );

txtTotal.Text = TotalCost.ToString() + " " + Tax.ToString();

The Adults and Children variables are ints and contain a quantity amount
from a form. The rate is grabbed from the database this is where the problem is everything after the decimal in the rates are being chopped off.

I know I am probably using the wrong types. What is correct way to do this?
Thank you very much, Justin.

Nov 18 '05 #3
I have tried using decimal, double and float but I get an error when I try to
multiply using these types, it wants to convert the numbers to ints. Heres
the new code:

int Adults = int.Parse(Request.Cookies["qntyAdult"].Value);
decimal PriceAdult = Convert.ToDecimal(myReader["RateAdult"]);

AdultTotal = Convert.ToDecimal(Adults) *
Convert.ToDecimal(myReader["RateAdult"]);

I can't believe something so simple has turned out to be difficult,
ahhhhhhh!!!!!!!

"ESPN Lover" wrote:
Int's don't carry any information beyond the decimal place. You could use
doubles or floats. Also you should add your Tax into the TotalCost to come
up with the GrandTotal.
"Justin" <Ju****@discussions.microsoft.com> wrote in message
news:73**********************************@microsof t.com...
I am trying to add dollar amounts together and add sales tax but everthing
after the decimal point is being cut off in the dollar amounts. Here is my
code:

if (Adults != "")
{
AdultTotal = int.Parse(Adults) * Convert.ToInt32(myReader["RateAdult"]);

ttlAdults.Text = Adults;
}

if (Children != "")
{
ChildTotal = int.Parse(Children) * Convert.ToInt32(myReader["RateChild"]);

ttlChildren.Text = Children;
}

double Tax = (AdultTotal + ChildTotal ) * (.06);
double TotalCost = (AdultTotal + ChildTotal );

txtTotal.Text = TotalCost.ToString() + " " + Tax.ToString();

The Adults and Children variables are ints and contain a quantity amount
from a form. The rate is grabbed from the database this is where the

problem
is everything after the decimal in the rates are being chopped off.

I know I am probably using the wrong types. What is correct way to do

this?

Thank you very much, Justin.


Nov 18 '05 #4
Hi Justin,

When using C#, it isn't necessary to use the Convert class to convert
integers to other numeric data types. Most of them can be implicitly cast.
Example:

int Adults = int.Parse(Request.Cookies["qntyAdult"].Value);
double PriceAdult = Convert.ToDouble(myReader["RateAdult"]);
double AdultTotal = ((double) Adults) * Adult;

ttlAdults.Text = Adults.ToString();

.... // Same for Children

double Tax = (AdultTotal + ChildTotal) * 0.06d; // Shouldn't need the "d"
but it's a good example
double TotalCost = AdultTotal + ChildTotal; // Same data type, no
casting necessary

txtTotal.Text = TotalCost.ToString("0.00") + " " + Tax.ToString("0.00");

Note the use of the formatting strings in the final statement. If you want 2
decimal places in the final string, you need to specify that there. Also,
mixing up the use of Decimal and Double isn't necessary or good. A Double
can hold a lot more precision than a decimal, so if you have doubts, use all
doubles. In any case, use all somethings (decimals, doubles, whatever floats
your boat), to make life (and your code) much simpler.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
I get paid good money to
solve puzzles for a living

"Justin" <Ju****@discussions.microsoft.com> wrote in message
news:B2**********************************@microsof t.com...
I have tried using decimal, double and float but I get an error when I try to multiply using these types, it wants to convert the numbers to ints. Heres
the new code:

int Adults = int.Parse(Request.Cookies["qntyAdult"].Value);
decimal PriceAdult = Convert.ToDecimal(myReader["RateAdult"]);

AdultTotal = Convert.ToDecimal(Adults) *
Convert.ToDecimal(myReader["RateAdult"]);

I can't believe something so simple has turned out to be difficult,
ahhhhhhh!!!!!!!

"ESPN Lover" wrote:
Int's don't carry any information beyond the decimal place. You could use doubles or floats. Also you should add your Tax into the TotalCost to come up with the GrandTotal.
"Justin" <Ju****@discussions.microsoft.com> wrote in message
news:73**********************************@microsof t.com...
I am trying to add dollar amounts together and add sales tax but everthing after the decimal point is being cut off in the dollar amounts. Here is my code:

if (Adults != "")
{
AdultTotal = int.Parse(Adults) * Convert.ToInt32(myReader["RateAdult"]);
ttlAdults.Text = Adults;
}

if (Children != "")
{
ChildTotal = int.Parse(Children) * Convert.ToInt32(myReader["RateChild"]);
ttlChildren.Text = Children;
}

double Tax = (AdultTotal + ChildTotal ) * (.06);
double TotalCost = (AdultTotal + ChildTotal );

txtTotal.Text = TotalCost.ToString() + " " + Tax.ToString();

The Adults and Children variables are ints and contain a quantity amount from a form. The rate is grabbed from the database this is where the

problem
is everything after the decimal in the rates are being chopped off.

I know I am probably using the wrong types. What is correct way to do

this?

Thank you very much, Justin.


Nov 18 '05 #5
Thank you very much!

For the sake of learning what the (double) do in ((double) Adults)?

Thanks, Justin.

"Kevin Spencer" wrote:
Hi Justin,

When using C#, it isn't necessary to use the Convert class to convert
integers to other numeric data types. Most of them can be implicitly cast.
Example:

int Adults = int.Parse(Request.Cookies["qntyAdult"].Value);
double PriceAdult = Convert.ToDouble(myReader["RateAdult"]);
double AdultTotal = ((double) Adults) * Adult;

ttlAdults.Text = Adults.ToString();

.... // Same for Children

double Tax = (AdultTotal + ChildTotal) * 0.06d; // Shouldn't need the "d"
but it's a good example
double TotalCost = AdultTotal + ChildTotal; // Same data type, no
casting necessary

txtTotal.Text = TotalCost.ToString("0.00") + " " + Tax.ToString("0.00");

Note the use of the formatting strings in the final statement. If you want 2
decimal places in the final string, you need to specify that there. Also,
mixing up the use of Decimal and Double isn't necessary or good. A Double
can hold a lot more precision than a decimal, so if you have doubts, use all
doubles. In any case, use all somethings (decimals, doubles, whatever floats
your boat), to make life (and your code) much simpler.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
I get paid good money to
solve puzzles for a living

"Justin" <Ju****@discussions.microsoft.com> wrote in message
news:B2**********************************@microsof t.com...
I have tried using decimal, double and float but I get an error when I try

to
multiply using these types, it wants to convert the numbers to ints. Heres
the new code:

int Adults = int.Parse(Request.Cookies["qntyAdult"].Value);
decimal PriceAdult = Convert.ToDecimal(myReader["RateAdult"]);

AdultTotal = Convert.ToDecimal(Adults) *
Convert.ToDecimal(myReader["RateAdult"]);

I can't believe something so simple has turned out to be difficult,
ahhhhhhh!!!!!!!

"ESPN Lover" wrote:
Int's don't carry any information beyond the decimal place. You could use doubles or floats. Also you should add your Tax into the TotalCost to come up with the GrandTotal.
"Justin" <Ju****@discussions.microsoft.com> wrote in message
news:73**********************************@microsof t.com...
> I am trying to add dollar amounts together and add sales tax but everthing > after the decimal point is being cut off in the dollar amounts. Here is my > code:
>
> if (Adults != "")
> {
> AdultTotal = int.Parse(Adults) * Convert.ToInt32(myReader["RateAdult"]); >
> ttlAdults.Text = Adults;
> }
>
> if (Children != "")
> {
> ChildTotal = int.Parse(Children) * Convert.ToInt32(myReader["RateChild"]); >
> ttlChildren.Text = Children;
> }
>
> double Tax = (AdultTotal + ChildTotal ) * (.06);
> double TotalCost = (AdultTotal + ChildTotal );
>
> txtTotal.Text = TotalCost.ToString() + " " + Tax.ToString();
>
> The Adults and Children variables are ints and contain a quantity amount > from a form. The rate is grabbed from the database this is where the
problem
> is everything after the decimal in the rates are being chopped off.
>
> I know I am probably using the wrong types. What is correct way to do
this?
>
> Thank you very much, Justin.


Nov 18 '05 #6
> For the sake of learning what the (double) do in ((double) Adults)?

It's a C# cast. Since Adults is an integer, it casts it as a double, which
is fine, as a double is larger in memory and more precise than an integer.

The rules for doing math can be kind of tricky. While you can often do math
on some different numeric data types without casting, the data type of the
result may or may not be the type you expect. It's therefore a good practice
to explicitly cast all operands to the same data type (the one you need for
a result) prior to operating on them.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
I get paid good money to
solve puzzles for a living

"Justin" <Ju****@discussions.microsoft.com> wrote in message
news:A9**********************************@microsof t.com...
Thank you very much!

For the sake of learning what the (double) do in ((double) Adults)?

Thanks, Justin.

"Kevin Spencer" wrote:
Hi Justin,

When using C#, it isn't necessary to use the Convert class to convert
integers to other numeric data types. Most of them can be implicitly cast. Example:

int Adults = int.Parse(Request.Cookies["qntyAdult"].Value);
double PriceAdult = Convert.ToDouble(myReader["RateAdult"]);
double AdultTotal = ((double) Adults) * Adult;

ttlAdults.Text = Adults.ToString();

.... // Same for Children

double Tax = (AdultTotal + ChildTotal) * 0.06d; // Shouldn't need the "d" but it's a good example
double TotalCost = AdultTotal + ChildTotal; // Same data type, no
casting necessary

txtTotal.Text = TotalCost.ToString("0.00") + " " + Tax.ToString("0.00");

Note the use of the formatting strings in the final statement. If you want 2 decimal places in the final string, you need to specify that there. Also, mixing up the use of Decimal and Double isn't necessary or good. A Double can hold a lot more precision than a decimal, so if you have doubts, use all doubles. In any case, use all somethings (decimals, doubles, whatever floats your boat), to make life (and your code) much simpler.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
I get paid good money to
solve puzzles for a living

"Justin" <Ju****@discussions.microsoft.com> wrote in message
news:B2**********************************@microsof t.com...
I have tried using decimal, double and float but I get an error when I try
to
multiply using these types, it wants to convert the numbers to ints.
Heres the new code:

int Adults = int.Parse(Request.Cookies["qntyAdult"].Value);
decimal PriceAdult = Convert.ToDecimal(myReader["RateAdult"]);

AdultTotal = Convert.ToDecimal(Adults) *
Convert.ToDecimal(myReader["RateAdult"]);

I can't believe something so simple has turned out to be difficult,
ahhhhhhh!!!!!!!

"ESPN Lover" wrote:

> Int's don't carry any information beyond the decimal place. You could use
> doubles or floats. Also you should add your Tax into the TotalCost
to come
> up with the GrandTotal.
>
>
> "Justin" <Ju****@discussions.microsoft.com> wrote in message
> news:73**********************************@microsof t.com...
> > I am trying to add dollar amounts together and add sales tax but

everthing
> > after the decimal point is being cut off in the dollar amounts.
Here is my
> > code:
> >
> > if (Adults != "")
> > {
> > AdultTotal = int.Parse(Adults) *

Convert.ToInt32(myReader["RateAdult"]);
> >
> > ttlAdults.Text = Adults;
> > }
> >
> > if (Children != "")
> > {
> > ChildTotal = int.Parse(Children) *

Convert.ToInt32(myReader["RateChild"]);
> >
> > ttlChildren.Text = Children;
> > }
> >
> > double Tax = (AdultTotal + ChildTotal ) * (.06);
> > double TotalCost = (AdultTotal + ChildTotal );
> >
> > txtTotal.Text = TotalCost.ToString() + " " + Tax.ToString();
> >
> > The Adults and Children variables are ints and contain a quantity

amount
> > from a form. The rate is grabbed from the database this is where

the > problem
> > is everything after the decimal in the rates are being chopped off. > >
> > I know I am probably using the wrong types. What is correct way to do > this?
> >
> > Thank you very much, Justin.
>
>
>


Nov 18 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

89
by: Radioactive Man | last post by:
In python 2.3 (IDLE 1.0.3) running under windows 95, I get the following types of errors whenever I do simple arithmetic: 1st example: >>> 12.10 + 8.30 20.399999999999999 >>> 1.1 - 0.2...
4
by: Robert Scheer | last post by:
Hi. Reading about the Math.random method I saw that by default it generates between 0 and 1. To generate numbers between a greater range I can use these syntaxes: x = Math.random()/10 x =...
40
by: Chiwa | last post by:
Hey, Expression: Math.floor(x * 100) / 100 x= 4.1 gives 4.09, why in gods name? While other values for x don't give a problem. Thx in advance
12
by: Woody Splawn | last post by:
I am trying to determine the age of a person based on two dates, the Date of Birth and Today(). I have a function that does this but it is kludgey and is giving me an age that is pretty close...
1
by: macklin01 | last post by:
Hi, everybody. I'm trying to do some last cleaning up on the following php page I wrote: http://www.math.uci.edu/~pmacklin/Publications.php This URL parses an XML file of publications: ...
2
by: Michael7 | last post by:
Hi everyone, I'm new to CSS of course, and have been trying to learn it. However, when I try to pull off something as simple as positioning of text . . . nothing works in my index page. So in...
22
by: giordan | last post by:
Hi all! I've wrote this code: <script type="text/javascript"> var largImg; var altImg; var txtTop = '<b>Ottima scelta!</b> Ora compila il form e premi "Ricevi banner". Il...
2
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - Why does simple decimal arithmetic give strange results?...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.