2

I'd like to format negative amounts with minus sign before currency symbol, i. e:

Given input value: -123.45

I want the output to be: -$123.45.

Is there any way to acomplish it with Joda Money?

Here's what I tried:

Money money = Money.of(CurrencyUnit.USD, -123.45);
MoneyFormatter formatter = new MoneyFormatterBuilder()
                 .appendCurrencySymbolLocalized()
                 .appendAmountLocalized()
                 .toFormatter(Locale.US);
String formatted = formatter.print(money);

But that gives me: $-123.45

Adamantium
  • 118
  • 7
  • Do you have code that you have tested? Joda Money does have methods like `isNegative()` so I assume that you are able to set it to be negative. – liquidsystem Dec 09 '15 at 19:12
  • Please put that into the main post, not into the comments. – liquidsystem Dec 09 '15 at 19:22
  • Sorry. I didn't know it would look so bad. – Adamantium Dec 09 '15 at 19:30
  • Are you able to call `.appendLiteral("-");` before `.appendCurrencySymbolLocalized();`? If so, I'd say to have two formatters, one for negative, and one for positive and use an if-test to check if it is negative or not. – liquidsystem Dec 09 '15 at 19:37
  • So why bother with Joda if in the end I have to implement formatting on my own? – Adamantium Dec 09 '15 at 19:41
  • You already use your own formatting...? That's what `MoneyFormatter` is doing. You would just be appending a negative sign in front of the rest of that code. – liquidsystem Dec 09 '15 at 19:42

1 Answers1

0

Joda Money 0.11 (released after your question was asked) added appendSigned to the formatter, which allows you to specify different formatters for negative, zero, and positive numbers. This will allow you to specify your own custom format for each case.