【问题标题】:How to floor a number to two decimal places?如何将一个数字保留到小数点后两位?
【发布时间】:2021-06-05 14:53:00
【问题描述】:

我正在尝试将双精度数据类型数字设置为小数点后两位,但当我输入 150 之类的数字时,我只能得到一位。

例如,如果我输入这个:

double num = 150.52152;

Math.floor(num * 100) / 100;

我得到150.52,没关系!

但是如果我输入 double num = 150 我会得到 150.0 而不是 150.00

我该如何解决这个问题?

【问题讨论】:

  • 150.0 与 150.00 相同,只是值如何呈现的问题。如果您想要特定的输出,则需要使用数字格式化程序

标签: java double decimal floor


【解决方案1】:

你可以通过两种方式做到这一点

  1. 使用String.format
String.format("%.2f",floorValue)
  1. 使用DecimalFormat
new DecimalFormat("#,##0.00").format(floorValue)

我建议您将new DecimalFormat("#,##0.00") 存储到final 变量中并在需要时使用它,而不是每次需要格式化时都创建一个新的DecimalFormat 对象。

演示:

import java.text.DecimalFormat;
import java.text.NumberFormat;

public class Main {
    public static void main(String[] args) {
        final NumberFormat format = new DecimalFormat("#,##0.00");// Do it once

        double floorValue = 150;
        System.out.println(format.format(floorValue));// Use it wherever you need to

        // Alternatively
        System.out.printf(String.format("%.2f", floorValue));
    }
}

输出:

150.00
150.00

【讨论】:

    【解决方案2】:

    控制小数位数是将小数转换为String 的功能。请记住,您正在查看以 2 为底的以 10 为底的值。并非每个此类值都是可表示的(例如 System.out.println(0.05+0.1);)。相反,使用String 格式。喜欢,

    System.out.printf("%.02f%n", 150.0);
    

    System.out.printf("%.02f%n", 0.05 + 0.1);
    

    【讨论】:

    • 非常感谢。我忘了提到我想在退货声明中打印它,比如“购买价值:”+ num……那我该如何应用同样的东西呢?
    • return String.format("Purchase value : %.02f", 150.0);
    • @PohovaniVodokotlic 看到这个stackoverflow.com/help/someone-answers
    【解决方案3】:

    要始终显示您的遮阳篷,并在小数点后显示两个值,您有两种输出方法。你的第一个如下:

    System.out.printf("%.2f", <YOUR VALUE>);
    

    %.2F 在一个浮点数中调用两个小数,该浮点数首先由 printf(打印浮点数)定义,因为整数不能是小数,所以不能删除。

    您的第二个选项是使用十进制格式。例如:

    // First define your fromat as decimalFormat
    DecimalFormat decimalFormat = new DecimalFormat();
    // Define number of digits after the decimal point in your case two
    decimalFormat.setMaximumFractionDigits(2);
    // Print out our result now formated in what we have just defined
    System.out.println(decimalFormat.format(<YOUR VALUE>));
    

    虽然你输出的是一个字符串,但它可以是一个浮点数(你可能需要在你的值后面加上“f”以确保它被定义)。

    希望这能提供信息。

    【讨论】:

    • round 是什么,在舍入 150 时如何显示 2 位小数?
    • 使用这个更简单System.out.printf("%.2f", val);
    • 什么是val? (我知道它是什么,但请发布一个完整且有用的答案)
    • 这不是我的问题,我知道如何处理这个问题。我要求编辑您的答案,使其成为正确且有意义的答案。
    • 这个答案现在是重复的,所以请删除它。
    【解决方案4】:

    您也可以这样做(但我更喜欢String.formatSystem.out.printf)。 请注意,不会显示尾随零,因此 100.10230 将显示为 100.1

    生成一些数据

    Random r =  new Random();
    double[] dbls = r.doubles(10).map(d->d*1000).toArray();
    

    打印值

    for (double d : dbls) {
        double df = (int)(d*100+.5)/100.;
        System.out.println(d + " --> " + df);
    } 
    

    打印类似的东西。

    738.2358190119878 --> 738.24
    902.7752505591593 --> 902.78
    467.26349962571953 --> 467.26
    503.2049813704648 --> 503.2
    20.288046322504805 --> 20.29
    409.10379837674714 --> 409.1
    559.2821881121522 --> 559.28
    288.5513513499909 --> 288.55
    124.07583289719682 --> 124.08
    535.1799354550361 --> 535.18
    

    【讨论】:

    • 尾随的零是问题所在...
    猜你喜欢
    • 2022-12-07
    • 2010-11-20
    • 1970-01-01
    • 2020-09-11
    • 2022-10-12
    • 2011-06-14
    • 1970-01-01
    • 1970-01-01
    • 2022-12-05
    相关资源
    最近更新 更多