Math.round()

Steps to reproduce: 1]Cost of item1:$20.95 Cost of item2:$7.99 Cost of item3:$18.99 Cost of Shipping:$4.99 Tax:10% Calculating the total cost i am using cents and then converting back to dollars :Math.round((2095+799+1899+499).1)/100=5.29 but this gives a different answer Math.round((2095+799+1899+499).1/100)=5 Can anyone explain what effects the outer bracket of Math.round ??
8 Replies
tototriou15
tototriou152w ago
You don't round the same number in the first one you round a number and divide by 100 AFTER, the second one you divide by 100 and then round so you don't have the same result
Vandana
Vandana2w ago
Ya i got what you said. But then second approach is valid as well to calculate the tax.
tototriou15
tototriou152w ago
yes but you won't have decimals in that case because round avoid it
Vandana
Vandana2w ago
Thanks.
tototriou15
tototriou152w ago
np
Vandana
Vandana2w ago
But then how will we get to know whether the answer must be in decimal or a whole number?
tototriou15
tototriou152w ago
use the first version and if it 5.2 you ll get 5.2 and if it's 5 you get 5.0 In one of my project I use "Number(number).toFixed(3)" to get 3 decimals
Vandana
Vandana2w ago
Okay got it thanks for the info.