Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Callum Davies
8,677 PointsRounding up to .47 and .97
I am trying to write a piece of code that will round a number up to the nearest .47 or .97 using the following code:
var regPrice = Math.ceil(minPriceMult * 2)/2 - 0.03
This code works for the most part. However for values of .48, .49, .98 and .99 it incorrectly rounds them down to the nearest .47 or .97 respectively rather than up. I am aware of why this happens; the .03 is subtracted after the rounding, however i have been unable to find a working alternative to the code above.
Any help on solving this problem would be appreciated.
1 Answer
Steven Parker
243,670 Points
You need to add first, then subtract after:
var regPrice = Math.ceil((minPriceMult + 0.03) * 2)/2 - 0.03