如何使用 Java 在没有科学记数法的情况下打印双精度值? [英] How do I print a double value without scientific notation using Java?

查看:26
本文介绍了如何使用 Java 在没有科学记数法的情况下打印双精度值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用 Java 打印一个没有指数形式的双精度值.

I want to print a double value in Java without exponential form.

double dexp = 12345678;
System.out.println("dexp: "+dexp);

它显示了这个 E 符号:1.2345678E7.

It shows this E notation: 1.2345678E7.

我希望它像这样打印:12345678

防止这种情况的最佳方法是什么?

What is the best way to prevent this?

推荐答案

You can use printf() with %f:

You could use printf() with %f:

double dexp = 12345678;
System.out.printf("dexp: %f
", dexp);

这将打印 dexp: 12345678.000000.如果您不想要小数部分,请使用

This will print dexp: 12345678.000000. If you don't want the fractional part, use

System.out.printf("dexp: %.0f
", dexp);

0 in %.0f 表示小数部分中有 0 个位置,即没有小数部分.如果您想打印具有所需小数位数的小数部分,那么只需提供类似 %.8f 的数字而不是 0.默认小数部分打印到小数点后 6 位.

0 in %.0f means 0 places in fractional part i.e no fractional part. If you want to print fractional part with desired number of decimal places then instead of 0 just provide the number like this %.8f. By default fractional part is printed up to 6 decimal places.

这使用了 文档.

在您的原始代码中使用的默认 toString() 格式被拼写出来 此处.

The default toString() format used in your original code is spelled out here.

这篇关于如何使用 Java 在没有科学记数法的情况下打印双精度值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆