问题
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