Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
For rounding up your floating point values use
Math.round()
function.
The below will return the value after two decimal places.
Math.round(doc['your_custom_type_var'].value * 100.0)/100.0
If you want to round up after 3 decimal places then change the value as like:
Math.round(doc['your_custom_type_var'].value * 1000.0)/1000.0
For your case do the followings :
Math.round((doc['cd'].value + 1) * 10.0 - 0.5 )/10.0 // -0.5 for getting the correct result. For this 45.401 and 45.601 both will return 45.6
Notes
Math.round()
function returns the closest int to the argument. For example
Math.round(45.40000152) // will return the value 45
Math.round(45.60000152) // will return the value 46
To get the correct answer you can substitute 0.5
to the actual number and then rounding up.Then it will return the value which we want to get.
First we multiple the value with 10.0 for moving the decimal place one unit right(for the above value 454.0000152
). After rounding this cuts of the floating point values(for the above value 454
), and so we divide the whole numbers by 10.0
for getting the value which rounded up by one decimal place from the actual value(for the above value 45.4
).
Think, it will help.
–
–
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.