Numeral-js: Wrong output

Created on 7 Mar 2019  路  1Comment  路  Source: adamwdraper/Numeral-js

Following code produces wrong output as 0$.00

console.log(numeral(-0.001).format('$0,0.00'));
// Outputs: 0$.00
  • Wrong output: 0$.00
  • Correct output should be $0.00

According to me following code in library is causing the issue

           if (value  >= 0) {
                symbols.before = symbols.before.replace(/[\-\(]/, '');
                symbols.after = symbols.after.replace(/[\-\)]/, '');
            } else if (value < 0 && (!numeral._.includes(symbols.before, '-') && !numeral._.includes(symbols.before, '('))) {
                symbols.before = '-' + symbols.before;
            }

Code should be changed to following

           if (parseFloat(output)  >= 0) {
                symbols.before = symbols.before.replace(/[\-\(]/, '');
                symbols.after = symbols.after.replace(/[\-\)]/, '');
            } else if (parseFloat(output) < 0 && (!numeral._.includes(symbols.before, '-') && !numeral._.includes(symbols.before, '('))) {
                symbols.before = '-' + symbols.before;
            }

Most helpful comment

Clearly a bug and @vinaykharecha already provided the solution.
What are you guys waiting for?

>All comments

Clearly a bug and @vinaykharecha already provided the solution.
What are you guys waiting for?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

chumager picture chumager  路  3Comments

rvanmil picture rvanmil  路  4Comments

garbinmarcelo picture garbinmarcelo  路  5Comments

jfstephe picture jfstephe  路  4Comments

MisterGoodcat picture MisterGoodcat  路  3Comments