Home > front end >  Convert Apostrophe to Comma by using numerljs formatter
Convert Apostrophe to Comma by using numerljs formatter

Time:09-07

I am using http://numeraljs.com/#format

I need formatter, which can covert number with apostrophe to comma.

Eg: 2'910'724'242 must change to 2,910,724,242

Is there any formatter available. Or we have to manually convert apostrophe to comma.

CodePudding user response:

It may not be the best way (I'm not familiar with the library) but, since you can't seemingly set the format on parsing, you can change the default:

let input = "2'910'724'242.6666";
numeral.defaultFormat("0'0.00");
let number = numeral(input);
let output = number.format("0,0.00");
console.log("Input: %s; Output: %s", input, output);
<script src="https://cdnjs.cloudflare.com/ajax/libs/numeral.js/2.0.6/numeral.min.js"></script>

CodePudding user response:

No need for a library

let str = `2'910'724'242`

let num = ( str.replaceAll("'","")).toLocaleString('en-US'); // replace(/'/g,"") is an alternative to the replaceAll

console.log(num)

str = `2'910'724'242.019999999`

num = ( str.replaceAll("'","")).toLocaleString('en-US', { minimumFractionDigits: 2, maximumFractionDigits: 2});

console.log(num)

But if you insist

let str = `2'910'724'242`

console.log(
  numeral( str.replaceAll("'","")).format('0,0')
)  

str = `2'910'724'242.0199999`

console.log(
  numeral( str.replaceAll("'","")).format('0,0.00')
)  
<script src="https://cdnjs.cloudflare.com/ajax/libs/numeral.js/1.0.3/numeral.min.js" ></script>

  • Related