Java:在BigDecimal中删除小数点后的零

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18721771/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 10:28:46  来源:igfitidea点击:

Java : Removing zeros after decimal point in BigDecimal

java

提问by Analysts

I have a program like this ,

我有一个这样的程序,

BigDecimal bd = new BigDecimal(23.086);
BigDecimal bd1= new BigDecimal(0.000);

bd = bd.setScale(2, RoundingMode.HALF_UP).stripTrailingZeros();
bd1 = bd1.setScale(2, RoundingMode.HALF_UP).stripTrailingZeros();

System.out.println("bd value::"+ bd);
System.out.println("bd1 value::"+ bd1);

I get the following output: 23.09for bdand 0.00 for bd1, but I want bd1as 0not as 0.00. Am I applying the methods correctly?

我得到以下输出:23.09forbd和 0.00 for bd1,但我想要bd10不是0.00. 我是否正确应用了这些方法?

采纳答案by Marcia Ong

BigDecimal bd  = new BigDecimal(23.086); 
BigDecimal bd1 = new BigDecimal(0.000);

bd  = bd.setScale(2, RoundingMode.HALF_UP).stripTrailingZeros(); 
bd1 = bd1.setScale(2, RoundingMode.HALF_UP).stripTrailingZeros();

System.out.println("bd value::"+ bd); System.out.println("bd1 value::"+ bd1);
System.out.println("new way:" + bd1.intValueExact());

//OUTPUT bd value::23.09

//输出bd值::23.09

bd1 value::0.00

bd1 值::0.00

new way:0

新方式:0

回答by arshajii

You can use printf()-- the %fspecifier works for BigDecimals too:

您可以使用printf()-- 说明%f符也适用于BigDecimals:

System.out.printf("bd1 value::%.0f%n", bd1);
bd1 value::0

回答by upog

try this

尝试这个

import java.math.BigDecimal;
import java.text.DecimalFormat;

public class calculator{
    public static void main(String[] args) {
        BigDecimal bd = new BigDecimal(23.086);
        BigDecimal bd1= new BigDecimal(0.000);    
        DecimalFormat df = new DecimalFormat("0.##");    
        System.out.println("bd value::"+ df.format(bd));
        System.out.println("bd1 value::"+ df.format(bd1));

    }

}

回答by Peter Lawrey

You can do this

你可以这样做

System.out.println("bd value: " + ((bd.scale() == 0) ? bd.unscaledValue() : bd));

回答by MrSimpleMind

Simple, clean, flexible, easy-2-understand and maintain code

简单、干净、灵活、易于理解和维护的代码

(will work for doubletoo)

会为工作double

DecimalFormat df = new DecimalFormat();
df.setMaximumFractionDigits(2); //Sets the maximum number of digits after the decimal point
df.setMinimumFractionDigits(0); //Sets the minimum number of digits after the decimal point
df.setGroupingUsed(false); //If false thousands separator such ad 1,000 wont work so it will display 1000

String result = df.format(bd);
System.out.println(result);

回答by Frdhsn

This method below works better IMO

下面的这种方法效果更好 IMO

    bd1 = bd1.stripTrailingZeros();
    System.out.println(bd1.toPlainString());

Prints:

印刷:

    0

If bd1 = 1.2300 , this will print

如果 bd1 = 1.2300 ,这将打印

    1.23

回答by Gondy

Let's say you have BigDecimal with value 23000.00and you want it to be 23000. You can use method stripTrailingZeros(), but it will give you "wrong" result:

假设您有一个带值的 BigDecimal23000.00并且您希望它是23000. 您可以使用 method stripTrailingZeros(),但它会给您“错误”的结果:

BigDecimal bd = new BigDecimal("23000.00");
bd.stripTrailingZeros() // Result is 2.3E+4

You can fix it by calling .toPlainString()and passing this to the BigDecimal constructor to let it handle correctly:

您可以通过调用.toPlainString()并将其传递给 BigDecimal 构造函数以使其正确处理来修复它:

BigDecimal bd = new BigDecimal("23000.00");
new BigDecimal(bd.stripTrailingZeros().toPlainString()) // Result is 23000