SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Dec 2006
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problem when multiplying integers

    Hello,

    I need to multiply a price by a quantity. I'm using the following:
    intTotal = (int(strPrice) * int(strQuantity))
    strPrice contains the value 20.80 and strQuantity contains the value 10
    It gives me a total of $200 when it should be $208. Why does it discard the .80 cents in the multiplication? What can I do to fix it?

    Thank you very much!

    Julie F.

  2. #2
    SitePoint Member
    Join Date
    Dec 2006
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    change it to
    intTotal = (cint(strPrice) * cint(strQuantity))

  3. #3
    SitePoint Member
    Join Date
    Dec 2006
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, it now multiplies the decimals but it rounds it up to the highest dollar. Example, if the answer is supposed to be $41.30, it rounds it up to $42. How can I prevent it from rounding up my numbers?

    Thanks a lot for your help!

    Julie F.

  4. #4
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Um, stuff with decimals should not be coerced/cast to be an integer (int), because by definition an integer is a whole number.

    try

    total = CDbl(strPrice) * CDbl(strQuantity)

  5. #5
    SitePoint Member
    Join Date
    Dec 2006
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    With the CDbl, it wouldn't display the trailing zero (it would display $40.8 instead of $40.80). After making a few more searches, I tried CCur but that did the samething. Finaly, I solved the issue with this:

    subtotal = CCur(strPrice) * CCur(strQuantity)
    <% = FormatCurrency(subtotal,2) %>

    Thanks and happy holidays!

    Julie F.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •