How do I get `NumberFormatter` to output parentheses for negative values?

64 views Asked by At

Fairly common usage, one wants negative numbers formatted with parentheses around them instead of a minus sign.

So I tried adding this:

    override fun toString(): String {
        return NumberFormatter.withLocale(Locale.ROOT)
            .notation(Notation.compactShort())
            .unit(currency)
            .precision(Precision.currency(Currency.CurrencyUsage.STANDARD))
 
            // NEW!
            .sign(NumberFormatter.SignDisplay.ACCOUNTING)
 
            .format(rawValue)
            .toString()
    }
Expected :A$ (23.89)
Actual   :-A$ 23.89

So it seems on the surface as if the API is there, but doesn't have any effect on the output.

So far, I have also tried:

  • .sign(NumberFormatter.SignDisplay.ACCOUNTING_NEGATIVE)
  • .sign(NumberFormatter.SignDisplay.ACCOUNTING_ALWAYS)
0

There are 0 answers