The right way to calculate tax #406
Unanswered
vresetnikov
asked this question in
Q&A
Replies: 3 comments 2 replies
-
Are you trying to calculate just the tax or the amount with tax? You shouldn't need the intermediate divide, you can just multiply by whatever precision you need: currency(212.41).multiply(0.22)
// {intValue: 4673, value: 46.73, s: {…}, p: 100} Alternatively the amount with tax: currency(212.41).multiply(1.22)
// {intValue: 25914, value: 259.14, s: {…}, p: 100} |
Beta Was this translation helpful? Give feedback.
0 replies
-
Hey Jason, thanks for the reply and for your help, much appreciated. Do you
think it is a good practice if I increase the precision 5 decimals for math
operations and then the last moment, right before storing the value into
the database, reinstantiate the value with the precision of 2 decimals?
Thank you.
…On Mon, Aug 1, 2022 at 4:46 PM Jason ***@***.***> wrote:
Are you trying to calculate just the tax or the amount with tax?
You shouldn't need the intermediate divide, you can just multiply by
whatever precision you need:
currency(212.41).multiply(0.22)
// {intValue: 4673, value: 46.73, s: {…}, p: 100}
Alternatively the amount with tax:
currency(212.41).multiply(1.22)
// {intValue: 25914, value: 259.14, s: {…}, p: 100}
—
Reply to this email directly, view it on GitHub
<#406 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AM6ZMDXGEQRV46GERXAGSW3VW7PKTANCNFSM55D4LUZA>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
Beta Was this translation helpful? Give feedback.
1 reply
-
Thank you. Last quick question, and forgive me if I am disturbing you this
much, I really appreciate your patience and kindness. As you have concisely
explained, there is no reason to do division separately and one should
rather do multiplication immediately. Looking to prevent potential future
misunderstandings, I would like to kindly ask you why is it that precision
suffers when doing it the elaborate way? ( currency(212.41).divide(100).
multiply(22) ) Thanks in advance
…On Mon, Aug 1, 2022 at 7:03 PM Jason ***@***.***> wrote:
So currency.multiply is a special case and one of the only methods that
does not truncate the passed in value before applying the operation, but
the resulting value will always have the correct precision. What you're
suggesting would likely work, but you may be doing more work than is
actually necessary.
|
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello, I have just bumped into a thing that got me puzzled somewhat. I was trying to get the 22% tax value of 212.41.
The operation: 212.41 / 100 outputs the following:
Now, with the precision set to 2 the last 2 digits 41 are dropped. As a result, after the final multiplication by 22 the result is 46.64 and not 46.73. I somewhat understand the root of the issue (probably the precision set to 2) but I would like to kindly ask if the current calculation and the loss of 9 cents is correct, or am I doing something wrong?
Maybe the following is wrong?
^ performing the operation above results in a 9 cents wipe, as .41 is dropped during the / 100 operation.
If it is wrong, should I increase the precision to say 4 or is there something else I should do? However, in terms of increasing precision, I really wouldn't want to do that because it messes up the user friendly money representation (2 digits). Thank you!
Beta Was this translation helpful? Give feedback.
All reactions