>Zombies Live in DecimalFormat


It must be June 13th, the Zombies are running around, feasting, and just in general messing everything up with their smelly drippy bodies. I swear they have invaded and found a nice pleasant home in Java’s DecimalFormat class. Particularly the format method. Now maybe I’m doing something completely wrong here, or maybe I’m expecting too much. However, I would expect the following simple test to pass:

Note: My interpretation of DecimalFormat assumed that if there was a Zero exponential value that it would not be displayed, otherwise it would. Thanks to the zombie squishers that corrected my interpretation.

public void testFloatFormat() throws Exception {
Float value = 1.0f; // Double's have the same issue
DecimalFormat format = new DecimalFormat("0.#######E0");
String result = format.format(value);
assertEquals("1", result);

But NOOOOOOOOOOOOOOO….what do I get in the result variable instead? “1E0”. The freaking “1E0” zombie has been causing me all sorts of nightmares with the PsychoPath XPath 2.0 processor. I finally hacked around the issue, but I would like to know if it is indeed a zombie infestation, or if there is some Bruce Campbell Chainsaw Welding Army of Darkness Voodoo (see picture at the top) that I need to do to get this to work. I’d even take a simple explanation as to why I’m getting these results. The Java Vodoo Machine (JVM) that is being used is Sun’s 1.6.13.

Oh well…with the hacks I put in place…an additional 165 tests are now passing for PsychoPath. One step closer to full compliance.

Zombies…I hate them.

Note: The long term solution is to use my own Chainsaw approach, and create a custom XPathDecimalFormat that extends DecimalFormat to combat what I consider to be zombies.

This entry was posted in bliteotw, eclipse, xml, zombies. Bookmark the permalink.

5 Responses to >Zombies Live in DecimalFormat

  1. Kim Sullivan says:

    >I don't get it, you're explicitly requesting scientific notation with E0 in the format specifier, and then you complain that you get it?How about dropping E0 from the format specifier? 😉

  2. sgl says:

    >Try:DecimalFormat("0.#######")orformat = new DecimalFormat("0");or evenformat = new DecimalFormat("0.#######E0 boo hoo the zombies have me")

  3. David Carver says:

    >As I said I probably could have have a brain fart. My interpretation of E0 was that if there wasn't an exponent (in this case the exponent would be dropped. The requirement I have is the following, that if there is no Exponent (i.e. a zero exponent) it should not show up otherwise it does.A workaround I could do I guess is do a check after formatting so to explicitly remove the E0.if (format.contains("E0")) format = format.replace("E0", "");The XPath 2.0 specifications tests require this type of format. Unfortunately you can't do E with out a 0 or put in a # to get around it.To me this is why I say their are Zombies in DecimalFormat.

  4. Kim Sullivan says:

    >I looked into the spec, and I think you might be doing the conversion wrong, but in a more fundamental way, because the requirements are much more complex than what DecimalFormat is able to handle.If I understand it correctly, the requirement is that if the number is between 1E-6 and 1E6 (which includes 1E0), it shouldn't display an exponent. So, you first need to check if the number falls into that range, and if it does use a decimal format like #####0.###### (actually, the spec says you're supposed to display it with as many digits as possible). Only if it falls outside that range, you use 0.0E0.

  5. David Carver says:

    >@Kim: Yeah so far it's not the number of decimal places that has been the problem. The format's I'm using depending on if it's a float, double, or bigdecimal value are being handled correctly. The nice thing is that the W3C has a Test Suite for XPath 2.0 and XQuery 1.0 that can be used to verify the implementations. As of right now all 695 Numeric Tests are passing.A couple of additional items I had to handle. NaN was formatting as ?, instead of NaN. Also had to handle formatting of Negative Zero as well.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s