4

我想用NumberFormat of Intl格式化貨幣,並在符號和數字之間用空格「」獲取返回的值。JS符號後的空格

new Intl.NumberFormat('pt-br', { style: 'currency', currency: 'USD' }).format(12345) 
// "US$12.345,00" 
new Intl.NumberFormat('pt-br', { style: 'currency', currency: 'BRL' }).format(12345) 
// "R$12.345,00" 

我想什麼: 「US $ 12.345,00」, 「R $ 12.345,00」

任何想法?

回答

2

您可以使用replace進一步格式化貨幣。

var usd = new Intl.NumberFormat('pt-br', { style: 'currency', currency: 'USD' }).format(12345).replace(/^(\D+)/, '$1 '); 
 

 
var euro = new Intl.NumberFormat('pt-br', { style: 'currency', currency: 'EUR' }).format(12345).replace(/^(\D+)/, '$1 '); 
 
var deEuro = new Intl.NumberFormat('de', { style: 'currency', currency: 'EUR' }).format(12345).replace(/^(\D+)/, '$1 '); 
 

 

 
console.log(usd); 
 
console.log(euro); 
 
console.log(deEuro);

+0

注意' '$''在字符串#第二個參數取代具有特殊的意義。 – tsh

+0

@RobG我更新了我的答案,以考慮不同的貨幣。 –

+0

貨幣是否總是在數字之前? –

相關問題