如何在java中用科学记数法表示数字? [英] How to express numbers in scientific notation in java?

查看:383
本文介绍了如何在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 10ˆ23

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

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中的浮点文字。简而言之,提供小数部分就好像它是科学记数法,而不是 x 10 ^ 23 你会写 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.67e−11

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

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