Money Format WTF
Posted by Uncle Bob on 01/06/2007
The reason the DailyWTF is so funny, is that we all secretly identify with it. Here’s my latest WTF.
It happened last night at about 7pm. My wife wanted me to run to the store with her. I wanted to get my tests to pass so I could check in my code. I knew that if I left the code checked out until morning, one of my compatriots would wake up at 3am and change something, and I’d have to do a merge. I hate doing merges!
I needed to write the toString() method for my Money object. I had my tests already. Here they are:
public void testToString() throws Exception {
assertEquals("$3.50", new Money(350).toString());
assertEquals("$75.02", new Money(7502).toString());
assertEquals("$0.01", new Money(1).toString());
assertEquals("$0.00", new Money(0).toString());
}
I quickly wrote the function I knew would work:
public String toString() {
return String.format("$%d.%02d",pennies/100, pennies%100);
}
What can I say. I’m an old C programmer. When the format method showed up in Java 5 I jumped for joy.
Even as I typed this code, something was nagging at the back of my brain. Something was telling me there was a better way. But then, I was interrupted by a huge disappointment.
It didn’t compile. Damn! I forgot I was writing in a Java 1.4 environment. No String.format!
What to do? What do to?
(Wife: “Bob, are you ready to leave yet? It’s getting late! The store is going to close!)
uncleBob.changeMode(CODE_MONKEY); I wrote this:
public String toString() {
int cents = pennies % 100;
int dollars = pennies / 100;
return "$" + dollars + "." + ((cents < 10) ? "0" : "") + cents;
}
The tests passed, and I checked in my code and went to the store with my lovely wife.
——-
This morning I woke up, finished reading a book on Quantum Mechanics, read a few blogs, and in general pursued my joyous life of study and work. But somethign was nagging at the back of my brain. Something told me to look in the Java Docs for NumberFormat.
(sigh). On one screen was the code my monkey brain had written last night. On the other screen was the JavaDoc for NumberFormat. (sigh).
So I sheepishly changed my code to:
public String toString() {
NumberFormat nf = NumberFormat.getCurrencyInstance();
return nf.format(pennies/100.0);
}
Of course I know Beck’s rule: Never let the sun set on bad code.
I really need to find that code monkey and kill it.
Comments
Carfield Yim 3 days later:
May be use NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.US); is better, otherwise it will pick the machine locale but actually Sun have make proper research about how people other than US format currency (Well, at least people at Hong Kong) and make silly guess about the format they use.
Nikolia 5 months later:
I have some problems with the code. It doesn’t work.
Pandora over 4 years later:
I don’t recommend that on any project that needs to work, but I also don’t recommend using a real product to learn your tools
Tenant Screening over 4 years later:
I wanted to get my tests to pass so I could check in my code. I knew that if I left the code checked out until morning, one of my compatriots would wake up at 3am and change something, and I’d have to do a merge.