reactpie 2017 reactpie 2017 -4 years ago 95
Javascript Question

Formatting negative currency ReactJS

I am new to React and I am using react-intl https://formatjs.io/react/v1/ library for formatting dates, currency. Currently, for the negative amount I get -$999.99, but I want it to look like this ($999.99)

How can I achieve this? This is what I am using currently

<IntlProvider locale='en-ca'>
<FormattedNumber value={someAmount} style='currency' currency='CAD' />
</IntlProvider>


Thanks in advance

Answer Source

I don't know how you're handling the $, but if it is added separately - meaning that someAmount is equal to -999.99 then you could write a simple convert function.

function format(num){
    return (num<0) ? '('+Math.abs(num)+')' : num;
}

Then call it

value={format(someAmount)}

Probably, your fuction will be defined in a React class so format: function(num){...} and you will need to call it accordingly, but I don't see the rest of your React module.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download