How to customize the form of rounding

只谈情不闲聊 提交于 2019-12-21 20:55:53

问题


My question may seem simple, but still can not get something that works. I need to customize the Math.round rounding format or something to make it work as follows:

If the number is 1.6 he should round to 1, if greater than or equal to 1.7 should round to 2.0 . And so to all other decimal results with # .6 The way I'm doing the 1.6 being rounded to 2 shall be rounded to 1.

How can I do that?

Thank you!


回答1:


Simply do this:

double threshold = 0.7;
Math.round(x - threshold + 0.5);



回答2:


You could write a method that takes a double variable as input and returns the integer based on the first digit after the point. For instance, you could convert the input to a String and delimit it at the decimal point. Then check if the first digit after the point is less or greater than 6.




回答3:


Math.floor(x + 0.6);

It possibly may solve your question.



来源:https://stackoverflow.com/questions/34634424/how-to-customize-the-form-of-rounding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!