问题描述
我正在编写一个处理行星质量和直径的程序;这些量以科学计数法表示.我的问题不是,请注意,不是如何以正确的方式打印大数字(那是使用 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中如何用科学计数法表示数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!