Java: Convert scientific notation to regular int
Asked Answered
H

6

20

How do I convert scientific notation to regular int For example: 1.23E2 I would like to convert it to 123

Thanks.

Hear answered 30/3, 2010 at 14:50 Comment(1)
Do you have the string "1.23E2" or a float that is equal to 1.23e2?Fovea
U
38

If you have your value as a String, you could use

int val = new BigDecimal(stringValue).intValue();
Unbecoming answered 30/3, 2010 at 14:53 Comment(4)
I used something like: String val = new BigDecimal(stringValue).toPlainString(); since I wanted the value back as String. Thanks for pointing me to BigDecimalHear
Fighting with some scientific-notation-formatted value from Excel (using POI) which happens to be a date in format "yyyyMMdd". This was really helpful!Toots
Old post but it's worth noting that the method new BigDecimal(stringValue).intValueExact() will only return int value if it fits in the range and has no fractional part. Otherwise it will throw ArithmeticException. Might avoid unexpected truncation/rounding.Talley
Not working for high range like, 5000000000Echinoderm
M
3

You can just cast it to int as:

double d = 1.23E2; // or float d = 1.23E2f;
int i = (int)d; // i is now 123
Megargee answered 30/3, 2010 at 14:52 Comment(2)
I actually tried this before posting the question here on stackoverflow but for some reason I decided not to use it since I was afraid of losing information during this conversion to Double and then to Int. But I guess it is a valid way.Hear
This is not valid. You are correct in thinking that some information will be lost. This is because doubles are 8 bytes while ints are 4 bytes. Take this as an example and notice the large difference after casting. double dlow = Double.parseDouble("-2299999999"); int ilow = (int) dlow; System.out.println("dlow="+String.valueOf(dlow)); System.out.println("(int)dlow="+String.valueOf(ilow)); System.out.println("ilow-dlow="+String.valueOf(ilow-dlow)); dlow=-2.299999999E9 (int)dlow=-2147483648 ilow-dlow=1.52516351E8Murmurous
M
2

I am assuming you have it as a string.

Take a look at the DecimalFormat class. Most people use it for formatting numbers as strings, but it actually has a parse method to go the other way around! You initialize it with your pattern (see the tutorial), and then invoke parse() on the input string.

Melt answered 30/3, 2010 at 14:54 Comment(0)
H
2

Check out DecimalFormat.parse().

Sample code:

DecimalFormat df = new DecimalFormat();
Number num = df.parse("1.23E2", new ParsePosition(0));
int ans = num.intValue();
System.out.println(ans); // This prints 123
Hygroscopic answered 30/3, 2010 at 14:57 Comment(0)
K
0

You can also use something like this.

(int) Double.parseDouble("1.23E2")
Knell answered 20/12, 2013 at 13:21 Comment(0)
C
-2

You can implement your own solution:

String string = notation.replace(".", "").split("E")[0]
Charlettecharley answered 21/9, 2020 at 15:49 Comment(1)
If the string looks like "1.23E4" you'll get 123 instead of 12300; or "1.234E1" will result in 1234 instead of 12. The string needs to be parsed properly using existing facilities mentioned in other answers for the past 10 years..Krystinakrystle

© 2022 - 2024 — McMap. All rights reserved.