I played around a little with the scientific notation as I already suggested in the comments. Here is what I came up with:
public static double simplify(Number n) {
String numberString = String.format("%e", n);
int indexE = numberString.indexOf('e');
String baseValue = numberString.substring(0, indexE);
String exponent = numberString.substring(indexE + 1);
double base = Double.parseDouble(baseValue);
int exp = Integer.parseInt(exponent);
return base * Math.pow(10, exp);
}
I used all numbers I found in your question and added a negative value as well to test it.
public static void main(String[] args) {
Number[] ns = new Number[]{
239999995.546,
239989995.546,
340000.007,
34.0000007,
5699999999.0,
235.00000002,
9875.999999997,
-4334.345345,
23500000001.0,
0.30000007,
-0.053999949
};
DecimalFormat df = new DecimalFormat("0.#####");
for(Number n : ns) {
String s = df.format(simplify(n));
System.out.println(" " + n + " is " + s);
}
}
The results are:
2.39999995546E8 is 240000000
2.39989995546E8 is 239990000
340000.007 is 340000
34.0000007 is 34
5.699999999E9 is 5700000000
235.00000002 is 235
9875.999999997 is 9876
-4334.345345 is -4334.345
2.3500000001E10 is 23500000000
0.30000007 is 0.3
-0.053999949 is -0.054
Edit I adjusted the code to use double, fixed the error with exponents < 0 and added another example. Additionally I plugged in a DecimalFormat
. Note that adding more #
might change some results, i.e. -0.053999949
will now show up as -0.054
, with more digits it will result in -0.05399995
.