- Intl.NumberFormat API
-
1.
Intl.NumberFormat([지역코드, {옵션 객체}]).format()은 각국의 언어에 맞는 숫자 서식에 맞게 표시해주는데, 달리 지역코드 및 옵션 을 주지 않으면; 해당 지역에 맞는 표기법으로 표시해준다: -
2.
원하는 지역과 옵션을 써서 Intl.NumberFormat 객체의 인스턴스를 만들고,
format()메서드에 숫자 를 전달해 적절한 형식의 숫자로 된 문자열을 얻을 수 있다: -
useGrouping: false천 단위 구분자 사용하지 않음 -
minimumInterDigits: n숫자의 정수 부분 자릿수 ← 실제 숫자보다 지정된 자릿수가 크면; 앞에 0 을 붙여서 맞춘다! -
minimumFractionDigits/maximumFractionDigits: n숫자의 소수점 아래 부분 최소(기본값: 0)/최대(기본값: 3) 자릿수 지정 ← 실제 숫자보다 지정된 자릿수가 크면; 뒤에 0 을 붙여서 맞추며, 작으면; 반올림한다!
const number= 3500
new Intl.NumberFormat().format(number) // 3,500 ← 기본값: 해당 지역에 맞는 표기법으로 표시된다!
new Intl.NumberFormat("ko-KR", {style: "currency", currency: "KRW"}).format(number) // ₩123,457 ← 한국 원화로 표기
☞
format() 메서드는 Intl.NumberFormat 객체와
결합된다(날짜, 문자열 등의 다른 국제화 관련 클래스도 마찬가지이다!).
따라서, 형식 객체를 참조하는 변수를 만들고 그 변수에서 format() 메서드를 호출할 필요 없이,
바로 변수에 할당하고 그 변수를 독립된 함수처럼 사용할 수 있다!
let pounds= Intl.NumberFormat("en", { style: "currency", currency: "GBP" })
console.log(pounds.format(1000)) // £1,000.00
☞
{옵션객체}의 첫번째 인자인 style: "값"으로는 decimal (정수.소수 형식 - 기본값)
percent (퍼센티지) 및 currency (화폐 형식)를 사용할 수 있는데,
currency 에서는 ISO 화폐코드 또한 지정해주어야 한다!
✓
{옵션객체}의 두번째 인자인 currency: "값" 에는 다음과 같은 옵션들을 사용할 수 있다:
- Intl.DateTimeFormat API
-
Intl.DateTimeFormat([지역코드, {옵션객체}]).format()은 각국의 언어에 맞는 날짜 및 시간 서식에 맞게 표시해주는데, 달리 지역코드 및 옵션 을 주지 않으면; 해당 지역에 맞는 표기법으로 표시해준다: year:numeric (4자리: 2005), 2-digit (항상 2자리: 05)month:numeric (가급적 짧은 숫자), long (전체 이름: January), short (약어: Jan)day:numeric (가급적 짧은 숫자), 2-digit (항상 2자리)hour,minute,second:numeric (가급적 짧은 숫자), 2-digit (항상 2자리)hour12:true / false (12시간제로 표시할 지 여부)
let date= new Date(Date.UTC(2025, 6, 7)) // 월은 0부터 시작한다!
console.log(new Intl.DateTimeFormat().format(date)) // 2025. 7. 7. ← 기본값: 해당 지역에 맞는 표기법으로 표시된다!
console.log(new Intl.DateTimeFormat("ko-KR").format(date)) // 2025. 7. 7. ← 한국에서는 년/월/일 순서
let options= { // 긴 날짜 서식에 더해 요일 추가
year: "numeric", month: "long", day: "numeric", weekday: "long"
}
console.log(new Intl.DateTimeFormat("ko-KR", options).format(today)) // 2025년 7월 7일 월요일
options= { // 숫자로 된 날짜와 시간
year: "numeric", month: "numeric", day: "numeric", hour: "numeric", minute: "numeric", second: "numeric"
}
console.log(new Intl.DateTimeFormat("default", options).format(today)) // 2025. 7. 7. 오후 9:29:36 ← 옵션을 지정하면서 지역은 브라우저 기본값을 사용할 때: 'default' 지정
options= { // 오전/오후 시간 표시가 필요할 때
hour: "numeric", dayPeriod: "short"
}
console.log(new Intl.DateTimeFormat("ko-KR", options).format(today)) // 오전 9시
✓ 옵션값 키워드 정리:
let date= new Date(Date.UTC(2025, 6, 7)) // 월은 0부터 시작한다!
console.log(new Intl.DateTimeFormat().format(date)) // 2025. 7. 7. ← 기본값: 해당 지역에 맞는 표기법으로 표시된다!
console.log(new Intl.DateTimeFormat("ko-KR").format(date)) // 2025. 7. 7. ← 한국에서는 년/월/일 순서
░ 나중에 더 필요해지면; 최신 API를 사용하는 날짜-시간 관련 JavaScript 라이브러리인 도 참조하십시오.. 이를 쓰면 좀 더 쉽고 다양하게 날짜/시간을 다룰 수 있습니다!
- Intl.Collator API
-
Intl.Collator 객체를 생성하고,
compare()메서드를sort()메서드에 전달하면; 지역에 적합한 순서로 정렬하는 것이 가능해진다 ←compare()메서드와sort()정렬 함수는 정확히 같은 방식으로 작동한다! -
usage: sort/searchCollator 객체를 사용하는 방법 지정 ← 기본값인 sort 는 엄격한 비교이며, search 는 느슨한 비교이다 -
sensitivity: base/accent/case/variant문자열 비교시 대소문자와 악센트를 감안할지 여부 지정 ← base 는 기본 글자만 비교하며, variant (이는sort()정렬에서의 기본값이다)는 악센트와 대소문자까지 엄격히 비교한다 -
ignorePunctuation: true공백과 구두점을 무시하고 비교한다 -
numeric: true정수 또는 정수가 포함된 문자열을 숫자 순서로 비교한다 -
caseFirst: upper/lower대문자/소문자를 앞에 둔다 ← 이는 배열의sort()메서드의 기본 동작이며, 대문자가 소문자보다 앞에 오는 유니코드 순서와는 다르다!
// 사용자의 지역에 맞게 정렬하는 기본 비교 함수
const collator= new Intl.Collator().compare
console.log(["a", "z", "A", "Z"].sort(collator)) // ['a', 'A', 'z', 'Z']
console.log(["다", "마", "가", "하"].sort(collator)) // ['가', '다', '마', '하']
// 파일 이름에 숫자가 포함되는 경우가 많으므로 따로 정렬해야 한다:
const f_name= new Intl.Collator(undefined, { numeric: true }).compare
console.log(["Page10", "Page5"].sort(f_name)) // ['Page5', 'Page10']
// 대상 문자열과 비슷한 문자열을 모두 찾는다:
const fuzzy= new Intl.Collator(undefined, { sensitivity: "base", ignorePunctuation: true }).compare
let str= ["food", "fool", "Foo Bar"]
console.log(str.findIndex(s => fuzzy(s, "foobar") === 0)) // 2 ← 찾은 요소의 인덱스번호
console.log(str.findIndex(s => fuzzy(s, "f00bar") === 0)) // -1 ← 찾지 못함
☞ 국제화 API의 첫번째 인자로 undefined 값을 주면; 기본값으로 설정된다!
✓
대부분의 전역객체와는 달리, Intl은 생성자가 아니므로
new 연산자와 함께 사용하거나 Intl 객체를 함수로 호출할 수 없다.
Math 객체와 마찬가지로 Intl의 모든 프로퍼티와 메서드는 정적이다!