Skip to main content
xenoveritas.org

Main navigation

  • Categories
  • Home
  • Links
  • Name Generator
  • Scramble
User account menu
  • Log in

Breadcrumb

  1. Home

java.text.DecimalFormat is broken...

By Xenoveritas, 19 April, 2006
Topics
Technology
  • Add new comment

From the Java 1.5 documentation:

If there is an explicit negative subpattern, it serves only to specify the negative prefix and suffix; the number of digits, minimal digits, and other characteristics are all the same as the positive pattern. That means that "#,##0.0#;(#)" produces precisely the same behavior as "#,##0.0#;(#,##0.0#)".

This is wrong. The negative pattern is, indeed, ignored - but it must be the exact same number of characters as the positive pattern.

But, hey, don't take my word for it. Try it!

import java.text.DecimalFormat;
public class FormatTest {
    public static void compareFormats(String goodFormat, String badFormat) {
        DecimalFormat good = new DecimalFormat(goodFormat);
        DecimalFormat bad = new DecimalFormat(badFormat);
        System.out.println("Comparing good <" + goodFormat + "> with the bad format <" + badFormat + ">");
        System.out.println("===========================================================================");
        System.out.println("Good: prefix=<" + good.getNegativePrefix() + ">; suffix=<" + good.getNegativeSuffix() + ">");
        System.out.println(" Bad: prefix=<" + bad.getNegativePrefix() + ">; suffix=<" + bad.getNegativeSuffix() + ">");
        System.out.println();
    }
    public static void main(String[] args) {
        compareFormats("#,##0.0#;(#,##0.0#)", "#,##0.0#;(#)");
        compareFormats("#,##0.0#;Let''s compare the #,##0.0# of characters", "#,##0.0#;Let''s compare the # of characters");
        compareFormats("#,##0.0#;(#,##0.0#)", "#,##0.0#;(01234567)");
        compareFormats("#,##0.0#;(#,##0.0#)", "#,##0.0#;(#akasdjf)");
    }
}

What do we get after running that?

Comparing good <#,##0.0#;(#,##0.0#)> with the bad format <#,##0.0#;(#)>
===========================================================================
Good: prefix=<(>; suffix=<)>
Bad: prefix=<(>; suffix=<>

Comparing good <#,##0.0#;Let''s compare the #,##0.0# of characters> with the bad format <#,##0.0#;Let''s compare the # of characters>
===========================================================================
Good: prefix=<Let's compare the >; suffix=< of characters>
Bad: prefix=<Let's compare the >; suffix=<racters>

Comparing good <#,##0.0#;(#,##0.0#)> with the bad format <#,##0.0#;(01234567)>
===========================================================================
Good: prefix=<(>; suffix=<)>
Bad: prefix=<(>; suffix=<)>

Comparing good <#,##0.0#;(#,##0.0#)> with the bad format <#,##0.0#;(#akasdjf)>
===========================================================================
Good: prefix=<(>; suffix=<)>
Bad: prefix=<(>; suffix=<)>

Of course. It eats the total number of format characters from the positive pattern from the suffix - if that leaves nothing, then the suffix becomes the empty string.

Great.

Current Games

I'm currently playing:

  • Final Fantasy XIV: Endwalker

I intend to maybe get around to finishing:

  • Literally anything else

Video Games Section

Thought for the Moment

I never have thoughts any more.

Old Thoughts

Has the LHC Destroyed the Earth?

  • Create new account
  • Reset your password
RSS feed
Powered by Drupal