JavaScript

Number.prototype.toLocaleString()メソッド


編集:2023.01.25 by T.A.

[Edge]。→

Number を指定の言語を考慮して書式化して文字列で返します。

構文
Number.toLocaleString([locales[, options]])
Number 数値を表す、オブジェクト、文字列、変数など。
locales 省略可。→Intl.NumberFormat():locales
options 省略可。→Intl.NumberFormat():options
戻り値

文字列、または NaN(Number の値が NaN だった場合)。

//Numberの指定
N=123.45;  //数値として。
D=N.toLocaleString('ja');  //日本語。
	//=[string]:123.45
N=Number("123.45");  //オブジェクトとして。
D=N.toLocaleString('ja');  //日本語。
	//=[string]:123.45
N="123.45";  //文字列として。
D=N.toLocaleString('ja');  //日本語。
	//=[string]:123.45
D="123.45".toLocaleString('ja');  //日本語。
	//=[string]:123.45
D="abc".toLocaleString('ja');  //日本語。
	//=[string]:abc
D=NaN.toLocaleString('ja');  //日本語。
	//=[string]:NaN
D=[10, 20].toLocaleString('ja');  //日本語。
	//=[string]:10,20

//locales
N="123.45";
D=N.toLocaleString('ja');  //日本語。
	//=[string]:123.45
D=N.toLocaleString('ar');  //アラビア語。
	//=[string]:123.45
D=N.toLocaleString('de');  //ドイツ語。
	//=[string]:123,45
D=N.toLocaleString('ja-u-nu-hanidec');  //日本語で、数値文字体系:漢字の数字。
	//=[string]:一二三.四五

//options
N="123.45";
D=N.toLocaleString('ja', {style:'currency', currency:'JPY'});  //日本語、スタイル:通貨、通貨国:日本。
	//=[string]:¥123
D=N.toLocaleString('ja', {style:'unit', unit:'degree'});  //日本語、スタイル:単位、単位:角度。
	//=[string]:123.45 度
D=[10, 20].toLocaleString('ja', {style:'currency', currency:'JPY'});  //日本語、スタイル:通貨、通貨国:日本。
	//=[string]:¥10,¥20

テスト