「JavaScript」toLocaleStringを使用して数値を指定した通貨単位に変換する

環境
Windows 10 home 64bit
Google Chrome 105.0.5195.127

構文
toLocaleString(locales, options)
引数
locales と options 引数で機能の動作をカスタマイズすることができ、アプリケーションが書式化の習慣を用いる言語を指定することができます。
戻り値
指定された数値の言語依存の表現による文字列です。

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
const tt = 1234;
const resultA = tt.toLocaleString('en-US', {
style: 'currency',
currency: 'USD'
});
console.log("ドル: "+resultA );
const resultB = tt.toLocaleString('ja-JP', {
style: 'currency',
currency: 'JPY'
});
console.log("円: "+resultB );
const tt = 1234; const resultA = tt.toLocaleString('en-US', { style: 'currency', currency: 'USD' }); console.log("ドル: "+resultA ); const resultB = tt.toLocaleString('ja-JP', { style: 'currency', currency: 'JPY' }); console.log("円: "+resultB );
const tt = 1234;
const resultA =  tt.toLocaleString('en-US', {
    style: 'currency',
    currency: 'USD'
});

console.log("ドル: "+resultA ); 

const resultB =  tt.toLocaleString('ja-JP', {
    style: 'currency',
    currency: 'JPY'
});

console.log("円: "+resultB );

実行結果

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
> "ドル: $1,234.00"
> "円: ¥1,234"
> "ドル: $1,234.00" > "円: ¥1,234"
> "ドル: $1,234.00"
> "円: ¥1,234"

 

JavaScript

Posted by arkgame