Question

I have a sequence of numbers like this:

1.687155E21
3.981457E19
0.5532155E21 
3.018843E21
2.0532155E21 
4.5532155E21
3.1637913E19

My problem is how to convert the two numbers which ends with 10^19 to be like the others (10^21). Because after this unification i need to trunc the number to print only something like 3.5.

In C/C++ i know how to work with precision, but in Java I haven't got any idea.

Was it helpful?

Solution

I'd suggest something similar as Tomasz Nurkiewicz did, but instead of dividing by 1E19 divide by 1E21, convert them to strings with the required precision using Formatter (see the comment of count0) but not as scientific format, but as a general one. In the end just add E21 to those strings. In the end you should get (I hope, I got the idea correctly)

1.687155E21
0.03981457E21
0.5532155E21 
3.018843E21
2.0532155E21 
4.5532155E21
0.031637913E21

OTHER TIPS

Divide all your number by / 1e19, round to as many decimal digits you want:

168.7155
3.981457
55.32155
301.8843
205.32155
455.32155
3.1637913

Use the Formatter Class to bring them into the desired scientific notation (java.util.Formatter)

Can't you just multiply the E19 numbers by 10 ^ 2 = 100?

After they have been normalized to E21, you should be able to divide all of them by 10^21 (if they're floats), and they will all be in the range of 0-9.999...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top