Intl.NumberFormat

Intl.NumberFormat 은 언어에 맞는 숫자 서식을 지원하는 객체의 생성자입니다.

시도해보기

생성자

Intl.NumberFormat() (en-US)

새로운 NumberFormat 객체를 생성합니다.

정적 메서드

Intl.NumberFormat.supportedLocalesOf() (en-US)

주어진 로케일 목록 중, 런타임이 지원하는 항목을 배열로 반환합니다.

인스턴스 속성

아래 속성은 Intl.NumberFormat.prototype에 정의되어 있으며 모든 Intl.NumberFormat이 이 속성을 공유됩니다.

Intl.NumberFormat.prototype.constructor

인스턴스 객체를 생성한 생성자 함수입니다. Intl.NumberFormat 인스턴스의 경우, 초기 값은 Intl.NumberFormat (en-US) 생성자입니다.

Intl.NumberFormat.prototype[@@toStringTag]

@@toStringTag 속성의 초기값인 문자열 "Intl.NumberFormat"입니다. 이 속성은 Object.prototype.toString()에서 사용합니다.

인스턴스 메서드

예제

기본적인 사용 방법

로케일을 지정하지 않고 사용하면 기본 로케일 및 기본 옵션 서식을 적용한 문자열을 반환합니다.

js
const number = 3500;

console.log(new Intl.NumberFormat().format(number));
// → 한국 로케일의 경우 '3,500' 표시

locales 사용하기

다음 예제는 지역화된 숫자 서식의 예시를 보입니다. 어플리케이션의 사용자 인터페이스 언어에 맞는 서식을 적용하려면 locales 매개변수로 적절한 언어(와, 필요한 경우 대체 언어)를 제공하는걸 잊지 마세요.

js
const number = 123456.789;

// 독일은 소수점 구분자로 쉼표를 사용하고 천 단위 구분자로 마침표를 사용
console.log(new Intl.NumberFormat("de-DE").format(number));
// 123.456,789

// 대부분의 아랍어 사용 국가에서는 실제 아라비아 숫자를 사용
console.log(new Intl.NumberFormat("ar-EG").format(number));
// ١٢٣٤٥٦٫٧٨٩

// 인도는 천, 라크(십만), 크로르(천만) 단위에 구분자 사용
console.log(new Intl.NumberFormat("en-IN").format(number));
// 1,23,456.789

// nu 확장 키로 기수법 지정 (아래 예시는 중국식 숫자 표기)
console.log(new Intl.NumberFormat("zh-Hans-CN-u-nu-hanidec").format(number));
// 一二三,四五六.七八九

// 발리어와 같이 지원되지 않을 수도 있는 언어를 지정할 때는
// 다음과 같이 대체 언어를 지정할 수 있음. 아래의 경우 대체 언어는 인도어
console.log(new Intl.NumberFormat(["ban", "id"]).format(number));
// 123.456,789

options 사용

options 매개변수를 지정해 결과를 원하는 형태로 바꿀 수 있습니다.

js
const number = 123456.789;

// 통화 서식
console.log(
  new Intl.NumberFormat("de-DE", { style: "currency", currency: "EUR" }).format(
    number,
  ),
);
// 123.456,79 €

// 한국 원화는 보조 통화 단위를 사용하지 않음
console.log(
  new Intl.NumberFormat("ko-KR", { style: "currency", currency: "KRW" }).format(
    number,
  ),
);
// ₩123,457

// 유효숫자를 세 개로 제한
console.log(
  new Intl.NumberFormat("en-IN", { maximumSignificantDigits: 3 }).format(
    number,
  ),
);
// 1,23,000

// 단위에 따라 형식 지정
console.log(
  new Intl.NumberFormat("pt-PT", {
    style: "unit",
    unit: "kilometer-per-hour",
  }).format(50),
);
// 50 km/h

console.log(
  (16).toLocaleString("en-GB", {
    style: "unit",
    unit: "liter",
    unitDisplay: "long",
  }),
);
// 16 litres

명세

Specification
ECMAScript Internationalization API Specification
# numberformat-objects

브라우저 호환성

BCD tables only load in the browser

같이 보기