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
tototriou
tototriou6mo 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
VandanaOP6mo ago
Ya i got what you said. But then second approach is valid as well to calculate the tax.
tototriou
tototriou6mo ago
yes but you won't have decimals in that case because round avoid it
Vandana
VandanaOP6mo ago
Thanks.
tototriou
tototriou6mo ago
np
Vandana
VandanaOP6mo ago
But then how will we get to know whether the answer must be in decimal or a whole number?
tototriou
tototriou6mo 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
VandanaOP6mo ago
Okay got it thanks for the info.
Want results from more Discord servers?
Add your server