java Java中将2e+08转换为整数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12769865/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert 2e+08 to integer in Java
提问by farissyariati
I'd like to know what the 2e+08
format in programming means?
I have some data related to project budget. How to convert it into integer in Java ?
我想知道2e+08
编程中的格式是什么意思?我有一些与项目预算相关的数据。如何在Java中将其转换为整数?
回答by arshajii
2e+08
means 2multiplied by 10^8. In other words, 2followed by 8zeros:
2e+08
表示2乘以10^8。换句话说,2后跟8 个零:
2e+08 = 200000000
2e+08 = 200000000
To convert it to an int
we can simply cast:
要将其转换为 anint
我们可以简单地转换:
int n = (int)2e+08
All of the following are equivalent in Java: 2e+08
, 2e08
, 2e8
, 2E+08
, 2E08
, 2E8
.
以下所有内容在 Java 中都是等价的:2e+08
, 2e08
, 2e8
, 2E+08
, 2E08
, 2E8
。
回答by David Heffernan
That number uses scientific notation. The e
signifies exponentiation, in this case to base 10. So this number is 2×108.
该数字使用科学记数法。的e
表示取幂,在这种情况下底座10因此,这数目是2×10 8。
Because calculators, computer programming languages etc. typically do not use superscript notation, e
is used to indicate the exponentiation.
因为计算器、计算机编程语言等通常不使用上标表示法,e
所以用指数表示。
To represent that number in Java, as an integer literal, write it like this:
要在 Java 中将该数字表示为整数文字,请这样写:
200000000
As @Saintali helpfully points out, in Java SE 7 and later you can use underscores in the literal to improve clarity:
正如@Saintali 指出的那样,在 Java SE 7 及更高版本中,您可以在文字中使用下划线来提高清晰度:
200_000_000
If the data you are reading uses scientific notation, then it represents floating point values. Should you really be converting this to integer data? If you do need to read this in from file and then convert to int, you should read into a floating point data type and then cast to int
.
如果您正在阅读的数据使用科学记数法,则它表示浮点值。您真的应该将其转换为整数数据吗?如果确实需要从文件中读取它然后转换为 int,则应该读入浮点数据类型,然后转换为int
.