Changed valueOf(double) algorithm. Thanks to Peti