java中如何用科学计数法表示数字?

2023-10-14Java开发问题
13

本文介绍了java中如何用科学计数法表示数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在编写一个处理行星质量和直径的程序;这些量以科学计数法表示.我的问题不是,请注意,不是如何以正确的方式打印大数字(那是使用 printf(),duh),我会如何...输入"这些数字,我猜你可以说.例如,汞的质量表示为:

I'm writing a program that deals with planets' mass and diameter; These quantities are expressed in scientific notation. My question is NOT, mind you, NOT how does one print large numbers the right way (That's using printf(), duh), its how I would... "type" these numbers, I guess you could say. For example, the mass of mercury is expressed:

3.30 x 1023

在我的行星质量数组中,一个元素看起来是:

And in my array of planet masses, an element would look:

33.0 * Math.pow(10, 23)

但是,我不认为这是正确的方法——它看起来会引发异常......那么从程序员的角度来看,我如何表达这样的大数字呢?谢谢!

However, I don't quite think this is the right way - it looks like it would throw an exception... So how could I express large numbers like that from a programmer's perspective? Thanks!

推荐答案

JLS 的第 3.10.2 节 讨论了 Java 中的浮点文字.简而言之,提供小数部分就好像它是科学记数法一样,但您应该写 e23:

Section 3.10.2 of the JLS talks about floating-point literals in Java. In short, provide the decimal part as if it were scientific notation, but instead of x 10^23 you would write e23:

3.30e23

要写一个负指数,您也可以轻松地为 6.67 x 10^(-11):

To write one with a negative exponent, you can do that easily also for 6.67 x 10^(-11):

6.67e11

这篇关于java中如何用科学计数法表示数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

如何使用 JAVA 向 COM PORT 发送数据?
How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)...
2024-08-25 Java开发问题
21

如何使报表页面方向更改为“rtl"?
How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)...
2024-08-25 Java开发问题
19

在 Eclipse 项目中使用西里尔文 .properties 文件
Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)...
2024-08-25 Java开发问题
18

有没有办法在 Java 中检测 RTL 语言?
Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)...
2024-08-25 Java开发问题
11

如何在 Java 中从 DB 加载资源包消息?
How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)...
2024-08-25 Java开发问题
13

如何更改 Java 中的默认语言环境设置以使其保持一致?
How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)...
2024-08-25 Java开发问题
13