I need to make sure that all my decimal numbers are always at most 15 characters long (including the dot), while keeping as much precision as possible. So, it must be 15 characters at maximum, including the "." the "E" for scientific notation and "-".
What I was thinking is to use scientific notation for large numbers, and use rounding for small numbers.
For example, for 1234567891234567.123456789 I would use scientific notation, but for 0.123456789123456789 I would just round it.
I looked for java libraries that would do this for me but I could not find any that lets me specify the total number of characters for the representation.
Grateful for any suggestions or pointers.
EDIT: some more thoughts - a number such as 0.000000000000024 can be represented with no loss by using 24000E-18. While 0.123456789123424 for example has to suffer some loss, which of course makes it harder to write any sort of simple branching algo.
EDIT2: the format that we use to the transmit data is alpha-numeric and limits the data to 15 characters total. I have to write code to satisfy the format so the data can be transmitted without triggering format errors, but while still keeping max precision possible within the limitation.
EDIT3: I am using this to test my functions, but so far everything fails for a number of cases:
Random rnd = new Random();
double n = 100000 + rnd.nextDouble() * 900000;
String res;
double rangeMin = -123456789123456D;
double rangeMax = 123456789123456D;
String val;
for (int i=1;i<=1000;i++) {
n = rangeMin + (rangeMax - rangeMin) * rnd.nextDouble();
val = Double.toString(n);
res = shorteningFunction(val);
System.out.println(val + " " + val.length() + " " + res + " " + res.length());
}