我尝试了很多方法使用 String.format() 和 DecimalFormater 将 Double 转换为精确的十进制字符串,到目前为止我发现了这一点:
String s = "-.0000000000000017763568394002505";
Double d = Double.parseDouble(s) * 100;
System.out.println((new java.text.DecimalFormat("###########################.################################################################################")).format(d));
我并不是很喜欢我的解决方案,因为制作一个无法涵盖所有情况的巨大模式对我来说很难看。
你还有其他方法吗?
我真的想要所有小数,没有科学记数法,没有尾随零。
谢谢
P.S.:我需要这个,不需要外部库,并且在 Java 1.7 中。
最佳答案
似乎您必须处理一些精度问题。 double 不适用于此用途。更好用BigDecimal
String value="13.23000";
BigDecimal bigD=new BigDecimal(value);
System.out.println(bigD);
System.out.println(bigD.stripTrailingZeros().toPlainString());
关于java - 有没有更好的方法将 Double 转换为其精确的十进制字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48804787/