简体   繁体   中英

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 . 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

( will work for double too )

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

bd1 value::0.00

new way:0

You can use printf() -- the %f specifier works for BigDecimal s too:

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 . You can use method stripTrailingZeros() , but it will give you "wrong" result:

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:

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

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

Prints:

    0

If bd1 = 1.2300 , this will print

    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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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