JavaScript

숫자λ₯Ό k, m, b, 천, 만, μ–΅ λ‹¨μœ„λ‘œ λ‚˜νƒ€λ‚΄κΈ°

presentKey 2023. 12. 12. 20:12

🚩 Intl

λ¬Έμžμ—΄, λ‚ μ§œ 및 μ‹œκ°„, 숫자 λ“± κ΅­μ œν™” 및 지역화 μž‘μ—…μ„ μˆ˜ν–‰ν•˜λŠ”λ° μ‚¬μš©λ©λ‹ˆλ‹€.

 

🚩 μ μš©ν•˜κΈ°

// 첫 번째 인자 locales: 지역
// 두 번째 인자 option
Intl.NumberFormat(locales, {
    notation: "compact", // 'compact'
    maximumFractionDigits: 1, // μ†Œμˆ˜ 자리 ν‘œν˜„
  }).format(num);

 

  // 1.2천
  console.log(Intl.NumberFormat('ko-KR', {
    notation: "compact",
    maximumFractionDigits: 1,
  }).format(1234));
  
  // 1.2μ–΅
  console.log(
    Intl.NumberFormat("ko-KR", {
      notation: "compact",
      maximumFractionDigits: 1,
    }).format(123456789),
  ); 
  
  // 1.2K
  console.log(Intl.NumberFormat('en-US', {
    notation: "compact",
    maximumFractionDigits: 1,
  }).format(1234));
  
  // 123.5M
  console.log(Intl.NumberFormat('en-US', {
    notation: "compact",
    maximumFractionDigits: 1,
  }).format(123456789));