• 解决方法1:

对Double类型的数字进行 格式化输出 ,相对来说不是很精确

  1. import java.text.DecimalFormat;
  2.  
  3. public class TestDouble_String {
  4. public static void main(String[] args) {
  5.  
  6. Double double1 = 123456789.123456789;
  7. DecimalFormat decimalFormat = new DecimalFormat("#,##0.00");//格式化设置
  8. System.out.println(decimalFormat.format(double1));
  9. System.out.println(double1);
  10.  
  11. }
  12. }
  • 解决方法2:

数字用BigDecimal表示,然后在输出string ,相对来说更精确

  1. import java.math.BigDecimal;
  2.  
  3. public class TestBigDecimal_String {
  4. public static void main(String[] args) {
  5. BigDecimal bigDecimal = new BigDecimal(Long.MAX_VALUE);
  6. String result = bigDecimal.toString();
  7. System.out.println(result);
  8.  
  9. }
  10. }

所以在要求显示的值更精确时采用第二种方式,当要求值显示的更直观时采用第一种方法

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-12-18
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-25
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-11-18
  • 2022-12-23
  • 2022-12-23
  • 2021-06-30
  • 2021-11-21
相关资源
相似解决方案