当我想要像#34; 1234.5678930E8F" 1234.5678930E8F"对于Long / BigDecimal / String格式/类型,我没有得到" 123456789300"例如:
Float test = 1234.5678930E8F;
BigDecimal testB = new BigDecimal( test, MathContext.DECIMAL64 );
System.out.println( testB );
System.out.println( test );
System.out.println( test.longValue() );
我出来了:
123456790528
1.23456791E11
123456790528
但我需要精确的价值。
答案 0 :(得分:0)
经过大量测试后,使用Double
代替Float
以获得更好的精度。正如约翰·博林格所说的那样。
Double testing = 1234.5678930E8;
BigDecimal testB = new BigDecimal( testing, MathContext.DECIMAL64 ).setScale(2, RoundingMode.CEILING);;
System.out.println( testB);
输出:
123456789300.00
在此Link
中查看有关舍入的详情