Have you ever tried to format a number into a negative dollar amount with cents like -$1,000.00?
If you have, I’m pretty sure you experienced some weird things with ColdFusion that you thought “should” work.
First off, let’s take a look at dollarFormat():
value = dollarFormat(-1000);
This will give the following: ($1,000.00)
Close, but instead of a minus sign before the $ sign, the whole amount is between parenthesis (mainly used to signify a negative amount in accounting).
Since dollarFormat() only takes 1 argument, that’s about all we can do with that.
Let’s try numberFormat():
value = numberFormat(-1000, ‘$_,___.__’);
This gives the following: $-1,000.00
Close, except for some reason the minus sign is after the $ sign instead of before it. I tried different variations passing in different format masks to numberFormat() but couldn’t make it format the way I want -$1,000.00
Adam Cameron has a solution:
value = createObject(“java”, “java.text.DecimalFormat”).init(“$##,####0.00;-$##,####0.00”).format(javacast(“double”,-1000));
He suggests to “use Java’s DecimalFormat which allows one to provide a formatting mask for both positive and negative numbers.”
The result is: -$1,000.00 which is exactly what I wanted! YAY!