在我的项目中,我从一个API获取字符串值,并需要将双精度值传递给另一个API。当我尝试将字符串值解析为双精度时,我无法获得原始数据。
以下是代码。
String l1="11352721345377306";
String l2="11352721346734307";
String l3="11352721346734308";
String l4="11352721346734309";
DecimalFormat df = new DecimalFormat(".00");
System.out.println(df.format(Double.parseDouble(l1)));
System.out.println(df.format(Double.parseDouble(l2)));
System.out.println(df.format(Double.parseDouble(l3)));
System.out.println(df.format(Double.parseDouble(l4)));
输出结果为:
11352721345377306.00
11352721346734308.00
11352721346734308.00
11352721346734308.00
出了什么问题?解析有问题吗?如何恢复原始值?
编辑:不使用Decimal Format:
1.1352721345377306E16
1.1352721346734308E16
1.1352721346734308E16
1.1352721346734308E16
BigDecimal
获取数值。BigDecimal dec = new BigDecimal(l4); System.out.println(df.format(dec));
- Thanigai Arasu