How to format negative amounts with Joda Money?

784 Views Asked by At

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

1

There are 1 best solutions below

0
On BEST ANSWER

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.