|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
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
|
|||
|
|||
|
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? =( |
|
#2
|
|||
|
|||
|
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. |
|
#3
|
|||
|
|||
|
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:
|
|
#4
|
|||
|
|||
|
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. |
|
#5
|
|||
|
|||
|
that's it... masking it. never thought of it. thanks
Quote:
|
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > ColdFusion Development > Addition of numbers not correct. |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|