How to express numbers in scientific notation in java?(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 中的浮点文字.简而言之,提供小数部分就好像它是科学记数法一样,但您应该写 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中如何用科学计数法表示数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:java中如何用科学计数法表示数字?
基础教程推荐
- 修改 void 函数的输入参数,然后读取 2022-01-01
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
- Struts2 URL 无法访问 2022-01-01
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
- 如何对 Java Hashmap 中的值求和 2022-01-01
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- 存储 20 位数字的数据类型 2022-01-01
- RabbitMQ:消息保持“未确认"; 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
