Basically, I'm curious on how to get hold of new BigDecimal(Double.toString(d)) without going through the process of creating a string.

The documentation for Double.toString is quite complex (and interesting). As I understand it, the method does not actually return the string representation of the number actually represented by the given double, but the string representation of the (near by) shortest real number that uniquely identifies the given double.

(I don't actually need this. If I did, I'd probably go through a string anyway. I'm just curious about this algorithm that finds this "short" real number that uniquely determines the given double value.)

If you're interested in the algorithm then the source of Double.toString() should be able to help, right?
–
Joachim SauerSep 8 '11 at 10:50

I'm curious if there is a more "mathematical" approach, (call it base-independent if you like), than going through though the base-10 string representation. See the "predecessor question" for a better context :-)
–
aioobeSep 8 '11 at 11:08

@Joachim: note that the source is closed in sun.misc package. We can of course decompile.
–
BalusCSep 10 '11 at 21:19

@BalusC: I'm pretty sure that there's equivalent code in the OpenJDK, so that would be a possible route.
–
Joachim SauerSep 11 '11 at 9:09

2 Answers
2

There is a new publication on this subject that may be of interest to those looking at providing solutions. It (Grisu2) works without bignums if you are willing to settle for the shortest string in 99.8% of cases, and an accurate but not shortest string in the remaining cases.