Double.toString()
I’ve been writing things in Java for a hell of a lot years now and few things come up in the base language that surprise me or mess me up - usually the only new things are external libraries and APIs. But today something caught me. There was a method I was responsible for that essentially converts database query results into XML. One of the fields was for money, and represented as a double (with varying amounts of decimal places due to it being the aggregate of other data). Normally using Double.toString() would give a normal number like “958725.48″. For some results though (larger ones) the string given by the toString() method was in scientific notation with an exponent, like “9.5872548E5″. So I checked the documentation for the method and sure enough the behaviour is documented:
If m is less than 10-3 or greater than or equal to 107, then it is represented in so-called “computerized scientific notation.”…
I can deal with things like this, but I just don’t get the rationale behind it - if you’re converting a double to a String representation, it’s not going to overflow since it returns a new String object. So in the end I had code like this:
NumberFormat nf = NumberFormat.getInstance();
nf.setGroupingUsed(false);
columnValue = nf.format(double);
I guess I learned something new, but it was still kind of a waste of time and if I was personally responsible for that method’s behaviour I would have made the conversion to scientific notation an option and not a conditional.
Victor Ng said,
July 28, 2006 @ 2:25 am
Isn’t there a decimal type that you should be using for money anyway?
You know what’s funny? I’m having grief to day with solaris - blastwave in particular. and being fed up - I googled for “blastwave sucks” and found you. hahaha
Luke said,
July 28, 2006 @ 7:15 am
There is a BigDecimal class but for SQL “Money” types the JDBC drivers I’ve used return Doubles to represent it, so using that would’ve meant just a different conversion instead of using NumberFormat. Both ways are an extra step where there shouldn’t be one (IMHO).
Yeah, you can Google a lot of “keyword sucks” queries and get this site. I’m such a positive guy! :-)
Robin Ward said,
July 28, 2006 @ 9:07 am
Remember once, I suggested that you change the name of your site to be Luke Hates Everything?
Luke said,
July 28, 2006 @ 9:10 am
Hate is such a strong word though. “Luke thinks everything sucks” would be more appropriate.
Ron said,
July 28, 2006 @ 10:24 am
You know, if you were looking to propel this blog into a higher echelon of popularity, that might be one such way to do it… but then you’d have to update more often :p
kelli said,
July 28, 2006 @ 10:23 pm
I have no idea what anything in this post means. That must be why i work for the gov’t.
NegZero said,
September 8, 2006 @ 7:19 pm
You did it. A full month without a post! August 2006 never existed in the land of Luke Reeves…
Luke said,
September 12, 2006 @ 8:50 pm
Yep, I am definitely one lazy bastard!
Athena said,
September 13, 2006 @ 7:52 pm
I came here just to say why not post more often — wondering what’s going on with you. . and then I saw another comment implying the same.
Then looked for your wife’s journal cause even if I can’t understand it, there’s usually some pretty and interesting pictures! But no link to that either.
Hope you two are well. All the best.
Luke said,
September 13, 2006 @ 7:57 pm
I’ll write something soon, but there’s definitely a link to Chie’s site off my front page :-)