当前位置:网站首页>Comparison of two BigDecimal data types, addition, subtraction, multiplication and division, and formatting

Comparison of two BigDecimal data types, addition, subtraction, multiplication and division, and formatting

2022-06-10 13:01:00 yechaoa

In general ,string For type comparison equals,int use =

and BigDecimal Need to use compareTo

if(goodsData.unitPrice.compareTo(new BigDecimal("0.00"))==0){
            etGoodsPrice.setText("");
        }else{
            etGoodsPrice.setText(String.valueOf(goodsData.unitPrice));
        }

The above code is just two BigDecimal Compare values of data types

new BigDecimal("0.00") Is to determine the data format , Two decimal places

==0, It's also described in the document ,0 It means equal ,-1 Say less than ,1 Greater than

Add, subtract, multiply and divide :

BigDecimal b=new BigDecimal(100);

Suppose a int value :int c=5;   If it is BigDecimal Type does not need to be converted

1. Add

b.add(BigDecimal.valueOf(c));

2. reduce

b.subtract(BigDecimal.valueOf(c));

3. ride

b.multiply(BigDecimal.valueOf(c));

4. except

b.divide(BigDecimal.valueOf(c));

format :

.setScale(2) Keep two decimal places , Default rounding , 1.235》1.24

.setScale(2,BigDecimal.ROUND_DOWN) Directly delete the decimal after two decimal places , 1.235》1.23

.setScale(2,BigDecimal.ROUND_HALF_UP) rounding , Take up , 1.235》1.24

.setScale(2,BigDecimal.ROUND_HALF_DOWN) rounding , Take down , 1.235》1.23

原网站

版权声明
本文为[yechaoa]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206101133345888.html