java 如何在Android中舍入浮点数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39078720/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to round float number in Android
提问by Long Dao
I am stuck in the scenario below:
我被困在下面的场景中:
If x is 1.5 or lower then the final result will be x = 1. If x is large than 1.5 then x = 2.
如果 x 为 1.5 或更低,则最终结果将为 x = 1。如果 x 大于 1.5,则 x = 2。
The input number will be x/100.
输入数字将为 x/100。
For instance: input = 0.015 => x = 1.5 => display x = 1.
例如:输入 = 0.015 => x = 1.5 => 显示 x = 1。
The problem I got is that float number is inaccurate. For example: input = 0.015 but actually it is something like 0.01500000000000002. In this case, x gonna be 1.500000000000002 which is large than 1.5 => display output is x = 2.
我遇到的问题是浮点数不准确。例如:input = 0.015 但实际上它类似于 0.01500000000000002。在这种情况下,x 将是 1.500000000000002,它大于 1.5 => 显示输出是 x = 2。
It happen so randomly which I don't know how to solve it. Like 0.5, 1.5 will give me the correct result. But 2.5, 3.5, 4.5, 5.5 will give me the wrong result. Then 6.5 will give me the correct result again.
它发生的如此随机,我不知道如何解决它。像 0.5,1.5 会给我正确的结果。但是 2.5、3.5、4.5、5.5 会给我错误的结果。然后6.5会再次给我正确的结果。
The code I implemented is below:
我实现的代码如下:
float x = 0.015;
NumberFormat nf = DecimalFormat.getPercentInstance();
nf.setMaximumFractionDigits(0);
output = nf.format(x);
So depends on x, the output might be right or wrong. It is just so random.
所以取决于 x,输出可能是对的,也可能是错的。它就是那么随机。
I alos tried to use Math.round, Math.floor, Math.ceils but none of them seems work since float number is so unpredictable.
我也尝试使用 Math.round、Math.floor、Math.ceils,但它们似乎都不起作用,因为浮点数是如此不可预测。
Any suggestion for the solution?
对解决方案有什么建议吗?
Thanks in advance.
提前致谢。
采纳答案by Enzokie
Here is my old code golf answer.
这是我的旧代码高尔夫答案。
public class Main {
public static void main(String[] args) {
System.out.println(math(1.5f));
System.out.println(math(1.500001f));
System.out.println(math(1.49999f));
}
public static int math(float f) {
int c = (int) ((f) + 0.5f);
float n = f + 0.5f;
return (n - c) % 2 == 0 ? (int) f : c;
}
}
Output:
输出:
1
2
1
回答by Jitesh Prajapati
to round a float
value f to 2 decimal places.
将float
值 f舍入到小数点后两位。
String s = String.format("%.2f", f);
to convert String
to float
...
转换String
为float
...
float number = Float.valueOf(s)
if want to round float
to int
then....
there are different ways to downcast float to int, depending on the result you want to achieve.
如果要舍入float
到int
then.... 有不同的方法可以将 float 向下转换为 int,具体取决于您想要实现的结果。
round (the closest integer to given float)
round(最接近给定浮点数的整数)
int i = Math.round(f);
example
例子
f = 2.0 -> i = 2 ; f = 2.22 -> i = 2 ; f = 2.68 -> i = 3
f = -2.0 -> i = -2 ; f = -2.22 -> i = -2 ; f = -2.68 -> i = -3
f = 2.0 -> i = 2 ; f = 2.22 -> i = 2 ; f = 2.68 -> i = 3
f = -2.0 -> i = -2 ; f = -2.22 -> i = -2 ; f = -2.68 -> i = -3
回答by MurugananthamS
You could use String.format
.
你可以使用String.format
.
String s = String.format("%.2f", 1.2975118);
回答by Rishi Swethan
I like simple answers,
我喜欢简单的答案,
Math.round(1.6); // Output:- 2
Math.round(1.5); // Output:- 2
Math.round(1.4); // Output:- 1
回答by Archana
I was facing the same problem, I used the DecimalFormat
. This might help you.
我遇到了同样的问题,我使用了DecimalFormat
. 这可能对你有帮助。
float x = 1.500000000000002f;
DecimalFormat df = new DecimalFormat("###.######");
long l = df.format(x);
System.out.println("Value of l:"+l);