Simple math is giving me trouble

Discussion in 'ASP .Net' started by =?Utf-8?B?SnVzdGlu?=, Oct 14, 2004.

  1. 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.
     
    =?Utf-8?B?SnVzdGlu?=, Oct 14, 2004
    #1
    1. Advertising

  2. =?Utf-8?B?SnVzdGlu?=

    Lloyd Dupont Guest

    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" <> wrote in message
    news:...
    >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.
     
    Lloyd Dupont, Oct 15, 2004
    #2
    1. Advertising

  3. =?Utf-8?B?SnVzdGlu?=

    ESPN Lover Guest

    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" <> wrote in message
    news:...
    > 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.
     
    ESPN Lover, Oct 15, 2004
    #3
  4. 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" <> wrote in message
    > news:...
    > > 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.

    >
    >
    >
     
    =?Utf-8?B?SnVzdGlu?=, Oct 15, 2004
    #4
  5. 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" <> wrote in message
    news:...
    > 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" <> wrote in message
    > > news:...
    > > > 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.

    > >
    > >
    > >
     
    Kevin Spencer, Oct 15, 2004
    #5
  6. 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" <> wrote in message
    > news:...
    > > 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" <> wrote in message
    > > > news:...
    > > > > 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.
    > > >
    > > >
    > > >

    >
    >
    >
     
    =?Utf-8?B?SnVzdGlu?=, Oct 15, 2004
    #6
  7. > 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" <> wrote in message
    news:...
    > 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" <> wrote in message
    > > news:...
    > > > 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" <> wrote in message
    > > > > news:...
    > > > > > 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.
    > > > >
    > > > >
    > > > >

    > >
    > >
    > >
     
    Kevin Spencer, Oct 18, 2004
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. chirs
    Replies:
    18
    Views:
    788
    Chris Uppal
    Mar 2, 2004
  2. AciD_X
    Replies:
    4
    Views:
    8,132
    Jonathan Turkanis
    Apr 1, 2004
  3. Mark Healey
    Replies:
    7
    Views:
    1,521
    Tim Prince
    May 22, 2006
  4. sixteenmillion

    The giving that keeps on giving

    sixteenmillion, Nov 19, 2007, in forum: C Programming
    Replies:
    0
    Views:
    444
    sixteenmillion
    Nov 19, 2007
  5. VK
    Replies:
    15
    Views:
    1,216
    Dr J R Stockton
    May 2, 2010
Loading...

Share This Page