ColdFusion Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreColdFusion Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Generate data entry and reporting .NET Web apps in minutes, straight from your database. Read our FREE whitepaper “Build Web 2.0 Applications Without Hand-Coding” Download now!
  #1  
Old August 22nd, 2004, 03:21 PM
globeuser globeuser is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Posts: 10 globeuser User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Unhappy Addition of numbers not correct.

Hello all,

I have a peculiar problem where if I were to add all the prices for a shopping cart system (using array sessions btw), the numbers are not quite right and off by a cent.

The weird thing I noticed that on some of the items that have say a xx.99 price and I use an odd quantity x item cost and add them to the cart, the grand total does not add up correctly.

I know the logic to add the items is correct because it works for the most part so it must be the way its stored as just string and not as real numbers.

I even used <cfset xxx = Evaluate(my math statement)>

Example:

Items: $461.95
Shipping: $13.00
Subtotal: $474.95

Tax1 (7%): $33.25
Tax 2 (7.5% of Items only): $34.65

YOUR TOTAL: CDN $542.84

Which is not right! it should be $542.85

help! what gives? =(

Reply With Quote
  #2  
Old August 22nd, 2004, 05:50 PM
kiteless kiteless is offline
Moderator
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jun 2002
Location: Raleigh, NC
Posts: 3,618 kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 4 Days 9 h 44 m 33 sec
Reputation Power: 53
Your math is incorrect:

461.95 + 13 = 474.95
474.95 * .07 = 33.2465
461.95 * .075 = 34.64625

474.95 + 33.2465 + 34.64625 = 542.84275

You've got to round each total. Not to sound harsh, but this is basic math.
__________________
Ask if you have a question, but also help answer questions that you have knowledge of! Thanks, Brian.
How to Post a Question in the Forums

Last edited by kiteless : August 22nd, 2004 at 05:52 PM.

Reply With Quote
  #3  
Old August 23rd, 2004, 10:19 AM
globeuser globeuser is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Posts: 10 globeuser User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Wink still not displaying properly

Hmm,

Interesting. I know this is basic math but I was wondering why when I do a DollarFormat(variable) to view the final result, it was $542.85 not $542.84 as u stated?

btw, i did exactly what u did as well and let me output the results and compare them side by side.

<cfset one = 461.95 + 13>
<cfset two = one * 0.07>
<cfset three = 461.95 * 0.075>

<cfset total = one + two + three>

<cfoutput>
One: #one# = #DollarFormat(one)#<br>
Two: #two# = #DollarFormat(two)#<br>
Three: #three# = #DollarFormat(three)#<br>

Grand Total: #total# = #DollarFormat(total)#
</cfoutput>

Results are:
-----------


One: 474.95 = $474.95
Two: 33.2465 = $33.25
Three: 34.64625 = $34.65
Grand Total: 542.84275 = $542.84

See what I mean? all people see are the dollar formatted one that should subtotal with an .X5 not .X4

I can clearly see that.

why does it round it up for the dollar format for two and three.

quirky I know but its driving me crazxy. it shouldn't be that difficult.

I stored the prices in a database as decimal and loaded it into an array but that should not be a factor.

thoughts?









Quote:
Originally Posted by kiteless
Your math is incorrect:

461.95 + 13 = 474.95
474.95 * .07 = 33.2465
461.95 * .075 = 34.64625

474.95 + 33.2465 + 34.64625 = 542.84275

You've got to round each total. Not to sound harsh, but this is basic math.

Reply With Quote
  #4  
Old August 23rd, 2004, 10:33 AM
kiteless kiteless is offline
Moderator
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jun 2002
Location: Raleigh, NC
Posts: 3,618 kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 4 Days 9 h 44 m 33 sec
Reputation Power: 53
Again, there is nothing odd going on here at all. This is all about rounding. You ask why it is rounding up on your example #2, and #3. Are you familiar with the rules of rounding? It seems to me that you are not, because if you were, you would see that 33.2465 *should* round to $33.25 because the 6 in the third decimal place causing the 4 in the second decimal place to round to a 5. The same goes for the other numbers. This isn't quirky at all, it's the way rounding has worked forever and should have been something everyone was exposed to in school.

If you want to round it off, then use the numberFormat() function in between each calculation, or better yet write up a short function that does this for you so you're not duplicating calls to numberFormat(). This produces the result that you are expecting because it rounds each long decimal off to two decimal places before adding up the final total:

<cfset one = 461.95 + 13>
<cfset two = one * 0.07>
<cfset three = 461.95 * 0.075>

<cfset total = one + numberFormat( two, '__________.__' ) + numberFormat( three, '__________.__' )>

<cfoutput>
One: #one# = #DollarFormat(one)#<br>
Two: #two# = #DollarFormat(two)#<br>
Three: #three# = #DollarFormat(three)#<br>

Grand Total: #total# = #DollarFormat(total)#
</cfoutput>

Last edited by kiteless : August 23rd, 2004 at 10:35 AM.

Reply With Quote
  #5  
Old August 23rd, 2004, 10:53 AM
globeuser globeuser is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Posts: 10 globeuser User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thumbs up thanks

that's it... masking it. never thought of it. thanks



Quote:
Originally Posted by kiteless
Again, there is nothing odd going on here at all. This is all about rounding. You ask why it is rounding up on your example #2, and #3. Are you familiar with the rules of rounding? It seems to me that you are not, because if you were, you would see that 33.2465 *should* round to $33.25 because the 6 in the third decimal place causing the 4 in the second decimal place to round to a 5. The same goes for the other numbers. This isn't quirky at all, it's the way rounding has worked forever and should have been something everyone was exposed to in school.

If you want to round it off, then use the numberFormat() function in between each calculation, or better yet write up a short function that does this for you so you're not duplicating calls to numberFormat(). This produces the result that you are expecting because it rounds each long decimal off to two decimal places before adding up the final total:

<cfset one = 461.95 + 13>
<cfset two = one * 0.07>
<cfset three = 461.95 * 0.075>

<cfset total = one + numberFormat( two, '__________.__' ) + numberFormat( three, '__________.__' )>

<cfoutput>
One: #one# = #DollarFormat(one)#<br>
Two: #two# = #DollarFormat(two)#<br>
Three: #three# = #DollarFormat(three)#<br>

Grand Total: #total# = #DollarFormat(total)#
</cfoutput>

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreColdFusion Development > Addition of numbers not correct.


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway