CODE
BÀI VIẾT
<input id="nhaptien" value="" /> <div id="ratien"></div> <script> const node = document.getElementById("nhaptien"); node.addEventListener("keyup", function(event) { var n = document.getElementById("nhaptien").value; const formatCash = n => { if (n < 1e3) return n; if (n >= 1e3 && n < 1e6) return +(n / 1e3).toFixed(2) + " ngìn"; if (n >= 1e6 && n < 1e9) return +(n / 1e6).toFixed(2) + " triệu"; if (n >= 1e9 && n < 1e12) return +(n / 1e9).toFixed(2) + " tỷ"; if (n >= 1e12) return +(n / 1e12).toFixed(1) + " ức"; }; document.getElementById("ratien").innerHTML = '<b style="font-size:25px;color:#ff4444;margin:10px 0px;display:block">' + formatCash(n) + '</b>'; }); </script>
Nhập vào input hiển thị số dạng rút ngắn theo đơn vị
Javascript
ihoan
đã viết 1 năm trước
Code này sẽ hiển thị ra số được rút ngăn theo đơn vị.
Ví dụ:
– 1000 = 1 ngìn
– 1000000 = 1 triệu
454
Bài viết cùng chủ đề
Hiển thị menu khi click chuột phải như ở màn hình máy tính
Cách để tạo một Modal, Popup bằng HTML, CSS, JS cơ bản
Code lịch hiển thị thời gian ngày tháng hiện tại bằng Javascript
Tạo hộp thời tiết bằng API openweathermap code Javascript
Bộ lọc select chọn Tỉnh Thành, Quận huyện, Xã phường, sử dụng data-id
Bộ lọc select tỉnh thành, quận huyện sử dựng API provinces.open-api.vn