how do you format a number to currency when using React native EXPO?

前端 未结 5 1573
深忆病人
深忆病人 2020-12-16 10:45

how do I take a number like 10000 and have it output as $10,000.00?

I even had a problem with String.format(...) with a

5条回答
  •  感情败类
    2020-12-16 11:02

    You can use toFixed method for showing 2 decimal point.

    let num = 1000; 
    console.log(num.toFixed(2)); // 1000.00
    

    And you can use Regex like this

    function currencyFormat(num) {
       return '$' + num.toFixed(2).replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1,')
    }
    console.log(currencyFormat(2665)); // $2,665.00

提交回复
热议问题