简体   繁体   English

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

[英]Java : Removing zeros after decimal point in BigDecimal

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.09 for bd and 0.00 for bd1 , but I want bd1 as 0 not as 0.00 .我得到以下 output: bd23.09bd1为 0.00,但我希望bd10而不是0.00 Am I applying the methods correctly?我是否正确应用了这些方法?

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

    }

}

Simple, clean, flexible, easy-2-understand and maintain code 简单,干净,灵活,易于2理解和维护代码

( will work for double too ) 也可以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);
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

You can use printf() -- the %f specifier works for BigDecimal s too: 您可以使用printf()%f说明符也适用于BigDecimal

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

Let's say you have BigDecimal with value 23000.00 and you want it to be 23000 . 假设您的BigDecimal值为23000.00而您希望它为23000 You can use method stripTrailingZeros() , but it will give you "wrong" result: 您可以使用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

你可以这样做

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

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

i create this method to fix my problem...我创建此方法来解决我的问题...

private BigDecimal removeZeros(BigDecimal bigDecimal){
            return new BigDecimal(bigDecimal.stripTrailingZeros().toPlainString());
        }

i don't like it, but do the job我不喜欢,但做这份工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM