【问题标题】:How to round float number in Android如何在Android中舍入浮点数
【发布时间】:2016-12-28 22:58:00
【问题描述】:

我陷入了以下场景:

如果 x 为 1.5 或更低,则最终结果将为 x = 1。 如果 x 大于 1.5,则 x = 2。

输入的数字将是 x/100。

例如: 输入 = 0.015 => x = 1.5 => 显示 x = 1。

我遇到的问题是浮点数不准确。例如: 输入 = 0.015 但实际上它类似于 0.01500000000000002。在这种情况下,x 将是 1.500000000000002,大于 1.5 => 显示输出为 x = 2。

它发生得如此随机,我不知道如何解决它。像 0.5 一样,1.5 会给我正确的结果。但是 2.5、3.5、4.5、5.5 会给我错误的结果。然后 6.5 将再次给我正确的结果。

我实现的代码如下:

float x = 0.015;
NumberFormat nf = DecimalFormat.getPercentInstance();
nf.setMaximumFractionDigits(0);
output = nf.format(x);

所以取决于 x,输出可能是对的,也可能是错的。就是这么随意。

我也尝试使用 Math.round、Math.floor、Math.ceils,但它们似乎都不起作用,因为浮点数是如此不可预测。

对解决方案有什么建议吗?

提前致谢。

【问题讨论】:

  • 告诉我:多少0.015000000000000020.01500000000000000 不同?是不是微不足道
  • 您是否尝试过 DecimalFormatter 将小数点后的数字限制为固定值? ...看到这个 - stackoverflow.com/questions/8895337/…
  • 就是这样。浮点数和双精度值的行为方式是这样的,因为并非所有小数都可以精确表示。如果您需要精确表示小数部分(例如,对于货币值),您可以使用 BigDecimal(但计算速度要慢得多)。
  • 和你在java中的round一样
  • 嗡嗡声似乎很困惑。所以如果我使用 BigDecimal,我会得到确切的值吗?问题在于,对于相同的输入,iOS 会执行正确的结果,而 Android 有时会给出正确的结果。

标签: java android double decimal rounding


【解决方案1】:

你可以使用String.format

String s = String.format("%.2f", 1.2975118);

【讨论】:

    【解决方案2】:

    我遇到了同样的问题,我使用了DecimalFormat。这可能会对您有所帮助。

    float x = 1.500000000000002f;
    DecimalFormat df = new DecimalFormat("###.######");
    long l = df.format(x);
    System.out.println("Value of l:"+l);
    

    【讨论】:

    • DecimalFormat 返回字符串伴侣。
    • 是的,DecimalFormat 仅用于指定您想要的格式。 format() 会处理它。检查输出是 long 而不是 String
    【解决方案3】:

    这是我的旧代码高尔夫答案。

    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;
        }
    
    }
    

    输出:

    1
    2
    1
    

    【讨论】:

    • 您好,这似乎是我需要的输出。你能解释一下你写的数学函数吗?我不完全明白。谢谢。
    • 先生是哪一部分?我也可以通过Skype解释:)
    • 我对使用 int c 和 float n 感到困惑?
    • int c 用作截断值,而float n 保存结果的完整值。由于您的目标是在数字大于 1.5 时进行舍入,因此我将检查是否有余数。我真的很抱歉我解释得很糟糕:(
    • 1.5 没有余数,但 1.5002 会有。
    【解决方案4】:

    float 值 f 舍入到小数点后 2 位。

    String s = String.format("%.2f", f);
    

    String 转换为float...

    float number = Float.valueOf(s)
    

    如果想将float 舍入为int 然后.... 根据您想要达到的结果,有多种方法可以将 float 向下转换为 int。

    round(与给定浮点数最接近的整数)

    int i = Math.round(f);
    

    例子

    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

    【讨论】:

    • 如果你有浮点数,比如说 0.00015 或 0.00025。在某些点上舍入,在某些点上舍入。仍然不是那么准确。无论如何感谢您的建议。
    【解决方案5】:

    我喜欢简单的答案,

    Math.round(1.6); // Output:- 2
    Math.round(1.5); // Output:- 2
    Math.round(1.4); // Output:- 1
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-16
      • 2015-09-07
      • 2022-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-09
      • 1970-01-01
      相关资源
      最近更新 更多