简体   繁体   English

十进制限制整体和小数部分

[英]Bigdecimal restriction whole and decimal part

How do you restrict the whole part to 15 and decimal part to 8 like 15.8 using BigDecimal 如何使用BigDecimal将整个部分限制为15,将小数部分限制为8,如15.8

For Example: 例如:

String quant= "1000";
String price = "123456789012345.12345678";
final int contant = 100;
BigDecimal bd1;
BigDecimal bd2;
String value = "";
bd1 = new BigDecimal(price).multiply(new BigDecimal(quant));
bd2 = bd1.divide(new BigDecimal(contant));
value = bd2.toPlainString();

Output value coming as 1234567890123451.2345678 . 输出值为1234567890123451.2345678 So can I restrict the whole part up to 15 and decimal part up to 8. 所以我可以将整个部分限制为15个,将小数部分限制为8个。

You can validate as follows 您可以验证如下

String price = "123456789012345.12345678";
    String[] str=price.split("\\.");
    if(!((str[0].length()==15)&&(str[1].length()==8))){
        System.out.println("not a valid format");
    }

If you restrict the 'whole' part to 15, then you will lose precision, why would you do that? 如果将“整个”部分限制为15,那么您将失去精度,为什么要这么做? You can restrict (round) the decimal part by using BigDecimal.setScale . 您可以使用BigDecimal.setScale来限制(舍入)小数部分。 setScale can take an integer (how many decimal parts should be used) and a rounding strategy, eg BigDecimal.ROUND_UP . setScale可以采用整数(应使用多少个小数部分)和舍入策略,例如BigDecimal.ROUND_UP

Check the JavaDoc from BigDecimal for further information. BigDecimal检查JavaDoc以获取更多信息。

Not sure why you would do that, but here is a way- 不知道为什么要这么做,但是这是一种方法-

int contant = 100;
String price = "123456789012345.12345678224423";
String quant = "1000";                
BigDecimal bd1 = new BigDecimal(price).multiply(new BigDecimal(quant));
BigDecimal bd2 = bd1.divide(new BigDecimal(contant));    

final int WHOLE_MAX = 15;
final int SCALE = 8;

String value = bd2.setScale(SCALE, RoundingMode.HALF_UP).toPlainString();

String tempWhole = value.substring(0, value.indexOf('.')); // Digits before the decimal point

String wholePart =   tempWhole.substring(0, Math.min(tempWhole.length(), WHOLE_MAX)); // Gets the first 15 digits
String decimalPart = value.substring(value.indexOf('.'));

value = wholePart.concat(decimalPart);
System.out.println(value);

Output: 输出:

123456789012345.23456782 123456789012345.23456782

The BigDecimal is described with precision. BigDecimal的描述非常精确。 You can set it using MathContext . 您可以使用MathContext进行设置。

MathContext context = new MathContext(15 + 8, RoundingMode.HALF_UP);

When you apply to your need 当您满足需要时

BigDecimal value = new BigDecimal("1234567890123456789.12345678",context);

The out put will be 输出将是

1234567890123456789.1235

This is the expected way that what is erased from the number is the smallest thing. 这是从数字中删除的内容最小的预期方式。 If you wan to set upper limit for the number you will need to declare your own type and check that result is grater then limit, then you have an overflow error. 如果您想为数字设置上限,则需要声明自己的类型,并检查结果是否大于限制,那么您将出现溢出错误。

You can implement your type that controls it by extending BigDecimal class 您可以通过扩展BigDecimal类来实现控制它的类型

private class BigNumber extends BigDecimal {

        private final int exponent;
        private final int mantissa;
        private BigDecimal limit;

        public BigNumber(String val, int exponent, int mantissa) {
            super(val);
            this.exponent = exponent;
            this.mantissa = mantissa;

            char[] limit = new char[exponent];
            Arrays.fill(limit, '9');

            this.limit = new BigDecimal(limit).add(BigDecimal.ONE);
        }

        @Override
        public BigDecimal add(BigDecimal augend) {

            BigDecimal result = super.add(augend);

            if(result.compareTo(limit) < 0) {
                return result;
            }

            throw new RuntimeException("Nuber to large");
        }
    }

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

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