Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> JavaScript

Cách in số có dấu phẩy phân cách hàng nghìn trong JavaScript

Để định dạng số có dấu phẩy phân cách hàng nghìn, dùng Intl.NumberFormat():

Ví dụ

<!DOCTYPE html>
<html>
  <body>
    <script>
      var number = 24287995;
      document.write(new Intl.NumberFormat().format(number));
    </script>
  </body>
</html>

Kết quả

24,287,995

Mở rộng: Intl.NumberFormat hỗ trợ locale: new Intl.NumberFormat('vi-VN').format(number) → 24.287.995 (dấu chấm theo chuẩn VN). Cũng hỗ trợ currency: new Intl.NumberFormat('vi-VN', {style:'currency', currency:'VND'}).format(number).