添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
率性的稀饭  ·  CN Home·  1 月前    · 
没读研的小蝌蚪  ·  北斗卫星导航系统·  5 月前    · 
不开心的金针菇  ·  JM Solution Sun Spray ...·  1 年前    · 
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.

Thank for your answer. But cd filed is dynamic value (ex: 44.2, 44.674). So, the value will be wrong when i applying your solution. I want to script_fields that will return to 45.2, 45.674 – Dat Truong Oct 6, 2016 at 6:23 You need to define the decimal point place you want to see. for example 1,2 or three. If you want to make dynamic position set. then use Math.pow(base, power) in the place of 10.0. Give feedback if any query – sunkuet02 Oct 6, 2016 at 6:45

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.