473,394 Members | 1,841 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,394 software developers and data experts.

Rounding Problems Calculating Sales Tax

loonette
I am calculating Sales Tax in a query. I need the sales tax to round up, but I've tried everything I've found on the internet and in this forum and I can't get anything to work. Here is what I am trying to do:
Expand|Select|Wrap|Line Numbers
  1. tax: (Switch([taxable]="y",([finalprice]*[taxrate]),[taxable]="n",([finalprice]*0)))
The Tax rate is set at 5.5% and say for instance the item sells for $1.00, the tax calculates to .055, which rounds up to $.06 in the form, but when you click in the box it shows up as .055 and makes the total 1.055. I need it to calculate as .06 and make the total 1.06. I tried to use this formula:
Expand|Select|Wrap|Line Numbers
  1. Expr1: CCur(Nz(Round([finalprice]*[TaxRate],2),0))
Just to see if I could get it to round and it worked, but it rounded down to $.05 instead of rounding up to $.06.

Any help would be greatly appreciated.
Apr 21 '10 #1

✓ answered by TheSmileyCoder

Could you not solve it by first rounding by the max precision?
Expand|Select|Wrap|Line Numbers
  1. Round(Round(FinalPrice*TaxRate,7),2)

18 4826
TheSmileyCoder
2,322 Expert Mod 2GB
@loonette
Not really sure what to say here. I tested it in Access and it rounds 0.055 up to 0.06. Why you would be getting a different result is beyond me.
Apr 21 '10 #2
Which formula did you try? Was it the first or second? Because the first rounds up to .06, the second rounds down to point .055. Below you can see what is going on on my form, it is calculating .605 and giving me a remainder of .01- when calculating the balance. Thanks!
Attached Images
File Type: jpg form.jpg (16.4 KB, 420 views)
Apr 21 '10 #3
TheSmileyCoder
2,322 Expert Mod 2GB
Well I tested it by writing Round(0.055,2) and got 0.06 then added Ccur(Round(0.55,2) and still got 0.06
Apr 21 '10 #4
NeoPa
32,556 Expert Mod 16PB
Like Smiley, I tried it out and consistently got 1.06. I had to amend your figures of course. The TaxRate must be 105.5% or 1.055 for this to work rather than 5.5%.
Apr 21 '10 #5
Okay, here is the formula I have in my query right now:
Expand|Select|Wrap|Line Numbers
  1. tax: CCur(Round((Switch([taxable]="y",([finalprice]*[taxrate]),[taxable]="n",([finalprice]*0))),2))
The result that I get when [finalprice]=$1.00 and [taxrate]=.055 is $.055.

What am I doing wrong? I need the tax to be a whole number and for it to round up.

BTW, How do you do the tag thing? Thx!
Apr 22 '10 #6
TheSmileyCoder
2,322 Expert Mod 2GB
I have tested what I can from here. If you want me to look further into this, you will need to attach the DB to the thread.
Apr 22 '10 #7
NeoPa
32,556 Expert Mod 16PB
Tags (generally) are done as matching pairs where the opening one is surrounded by [...] and the closing one by [/...]. A set of buttons is available for ease of use in the Standard Editor (Not the Basic Editor). The one for the [code] tags has a hash (#) on it. You can choose which editor to use in your Profile Options (Look near the bottom of the page). You must select the code in your post, and hit the button on the tool bar which looks like a hash (#). This will automatically format the post such that the [code] tags surround the code you're posting. This will display the code in a Code Box, quite differently from the standard formatting of a post.
Apr 22 '10 #8
NeoPa
32,556 Expert Mod 16PB
Your formula has an unnecessary set of parentheses () which seems to have confused you. The ,2) parameter for the Round() function is not matching in the correct set. Try :
Expand|Select|Wrap|Line Numbers
  1. tax: CCur(Round(Switch([taxable]="y",([finalprice]*[taxrate]),[taxable]="n",([finalprice]*0)),2))
Apr 22 '10 #9
Neopa,

I made the change to formula that you suggested, but it is still rounding down to .05, instead of rounding up to .06.

This is very frustrating. I guess I will go back to the original formula it was at least rounding up to .06.

Thanks for you help.
Apr 22 '10 #10
NeoPa
32,556 Expert Mod 16PB
This rounding problem is so strange. In the normal course of events this simply doesn't happen. I'm coming to the conclusion that something else that we don't know about is affecting the result, or maybe even just the appearance of the result. I cannot guess what this may be for now, so let me ask you to run a test for me :

First you need to go to the Immediate Pane. To get there from the main Access window simply press Alt-F11 to open/switch to the Integrated Development Environment (or VBA Editor), then press Ctrl-G to open/switch to the Immediate Pane.

When there paste in the following code and then copy & paste the result (including the code too please) back into a post in here for me to review :
Expand|Select|Wrap|Line Numbers
  1. ?Round(1.055,2),Round(-1.055,2)
Apr 23 '10 #11
Stewart Ross
2,545 Expert Mod 2GB
Hi all. This is a classic example of small errors in the representation of floating-point numbers causing rounding problems. Loonette does not say what the data type of the field storing the tax rate is - double would be better than single but even so this will not affect the basic problem, which is inexact representation of decimal values in a floating-point form.

I am assuming that the data type used for the tax rate is single here, as it demonstrates the problem very clearly. I would not use single-precision values in any circumstances, as seven significant figures is just too low for most purposes, but doubles are not immune either (even with 15 significant figures).

Setting the tax rates to values on the rounding threshold and calculating a resultant sales price shows up the representation problem:

Expand|Select|Wrap|Line Numbers
  1. Final Price  Tax Rate  Non-Rounded        Rounded
  2. £1.00        1.015     1.01499998569489   1.01
  3. £1.00        1.025     1.02499997615814   1.02
  4. £1.00        1.035     1.0349999666214    1.03
  5. £1.00        1.045     1.04499995708466   1.04
  6. £1.00        1.055     1.05499994754791   1.05
  7. £1.00        1.065     1.06500005722046   1.07
  8. £1.00        1.075     1.07500004768372   1.08
  9. £1.00        1.085     1.08500003814697   1.09
  10. £1.00        1.095     1.09500002861023   1.1
Note that the calculation of the non-rounded sales price is lower than expected for values of 1.055 and under, but higher than expected for those above. The rounding is working correctly on what it is being fed.

Changing the tax rate data type to Double resolves this problem, at least for this range of values. A more general solution to it is to add to the value being rounded a small amount which will ensure it rounds correctly. Normally such a value is around half of the least-significant figure in the base calculation. In this case, adding 0.00005 to the figure being rounded as follows will also give correct results:

Result = round(FinalPrice * TaxRate + 0.0005, 2)

Conclusion: use the highest-precision data type you can, but even so recognise that representing decimal values in floating-point form is inexact, and the consequences have to be dealt with systematically when encountered. Use rigorous testing of a range of values to ensure that there are no adverse consquences from whatever solution is adopted.

Stewart
Apr 23 '10 #12
TheSmileyCoder
2,322 Expert Mod 2GB
Could you not solve it by first rounding by the max precision?
Expand|Select|Wrap|Line Numbers
  1. Round(Round(FinalPrice*TaxRate,7),2)
Apr 23 '10 #13
NeoPa
32,556 Expert Mod 16PB
I don't believe there is a solution as such. We are dealing with an arithmetic approximation which means that recognising an exact figure (anything.??5) is not always possible.

Actually, an actual solution could be managed by using different variable types, but would be a bit messy. Smiley's proposed solution is probably the tidier, as it doesn't assume the result is low, but either would be practically workable.

As Stewart says, a decimal variable would handle the situation, or another more kludgy way would be an integer (or Long) variable multiplied, then rounded, then divided to handle the decimal places.

Anyway, it was a good spot by Stewart :)
Apr 23 '10 #14
Stewart Ross
2,545 Expert Mod 2GB
To answer Smiley's point, rounding the intermediate stage to seven places will not in general help, as the inherent approximation involved is causing errors from the fourth place onwards - any minor rounding at, say digit 7 or 6 will have no effect on the erroneous value of digit 4 unless it pushes the ...999 parts to round up. This will be the case for the thresholds shown, but for general rounding errors it is not going to give predictable results.

Further, if single-precision is used to store the result then rounding to seven places can have no effect on the value, which is already at maximum precision.

-Stewart
Apr 23 '10 #15
ADezii
8,834 Expert 8TB
@NeoPa
This could be one of your Kludgies! (LOL)
Expand|Select|Wrap|Line Numbers
  1. Public Function MyRound(varValue As Variant, intDecimalPlaces As Integer)
  2.   MyRound = Int((varValue * intDecimalPlaces) + 0.5) / intDecimalPlaces
  3. End Function
'
Expand|Select|Wrap|Line Numbers
  1. 'Round to 2 Decimal Places
  2. Debug.Print MyRound(1.055,100) produces ==> 1.06
Apr 24 '10 #16
Thanks a million guys, I used Smiley's formula in post 13 and it worked! I didn't realize I was going to start such a debate, but I really appreciate all of your effort to help me fix this problem. Take care!
Apr 25 '10 #17
TheSmileyCoder
2,322 Expert Mod 2GB
I have given this some more though, and realise that the suggestion I gave will stop working for larger numbers. I therefore amend it to:
Round(Round(FinalPrice*TaxRate),3),2)

Now it is only basing the rounding in the signaficant figures (unless you exceed 1 million in cost, in which case I doubt anyone will give a flying rats arse about whether the rounding of the last cent was off :)

I can't immediatly think of any place where this might fail. Finally Stewart, im impressed you caught this one :)
Apr 26 '10 #18
Stewart Ross
2,545 Expert Mod 2GB
(Blush) Thanks Smiley!

I'm glad that Loonette's problem is now resolved, and with the simple double rounding you've provided rather elegantly too.

-S
Apr 26 '10 #19

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Dalan | last post by:
Is there any code available to address currency rounding problems in Access 97? Apparently, selecting currency type table fields does not resolve the problem. For instance, in my form I have a...
4
by: Jackson Peebles | last post by:
Hey everyone! I'm a complete newbie to PHP, and am trying to teach myself how to make some scripts. So far I've done pretty good, but even after searching through all my books, articles, manuals,...
3
by: Wired Hosting News | last post by:
Lets say I have 10 products in 10 different stores and every week I get a report from each store telling me how many items they have left for each of the 10 products. So each week I enter in 100...
5
by: Wired Hosting News | last post by:
I tried to be breif and give a scenario so as not to be overlooked because it was soooo long. Let me give you real world. I am a manufacturer of goods and produce 11 items that are distributed...
5
by: steve | last post by:
Hi All Not sure if this is the right forum, but I need the formula for calculating the amount of Sales tax (GST) from the tax included price In Australia GST is 10% and the standard formula is...
5
by: Cygnus | last post by:
Sorry in advance for the lack of formatting in this posting. Data: (column headers) Net Sales | Royalty Rate | Total Royalty (data) 4.31 | 50.00% | 2.15 19.35 | 50.00% | 9.68
2
by: Amol | last post by:
This is the part of the problem that I want to solve ..........The rounding rules for sales tax are that for a tax rate of n%, a shelf price of p contains (np/100 rounded up to the nearest...
206
by: md | last post by:
Hi Does any body know, how to round a double value with a specific number of digits after the decimal points? A function like this: RoundMyDouble (double &value, short numberOfPrecisions) ...
2
by: bagerbeiger | last post by:
I am relatively new to programming (3-5 months) and have to write a program that prompts users for input (number of items ordered at a restaurant), performs some monetary calculations, and then...
2
by: RZ15 | last post by:
Hi guys, I'm really drawing a blank here for how to deal with fiscal months in my monthly sales/receipts reports. My issue is that calculating the months is not as simple as saying 'if the invoice...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.