1

私は次のように使用しBigDecimalています:

BigDecimal val = object.getValue();//it returns BigDecimal type value

val12.2300 が含まれています。ページに 12.23 を表示したい。これどうやってするの?

4

2 に答える 2

4

使用DecimalFormat:

String formatted = new DecimalFormat("#.##").format(val);
于 2010-12-16T06:21:43.260 に答える
1

DecimalFormatを使用するか、文字列操作で操作できます

        public static String format(BigDecimal big, int precision){
            String bigS = big.toString();
            int index = bigS.indexOf('.');
            String add = bigS.substring(index, index + precision + 1);
            String formatted = big.longValue() + "" + add;

            return formatted;
        }

        public static void main(String[] args) {

            BigDecimal big = new BigDecimal(2342342232.23232);
            System.out.println(format(big, 2));
            System.out.println(format(big, 3));
        }

もちろん、DecimalFormatは賢い方法です

于 2010-12-16T06:36:17.680 に答える