本文介紹了如果一千或更多,則將數字格式化為 2.5K,否則為 900的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
限時送ChatGPT賬號..
我需要以 1K
等于一千的格式顯示一個貨幣值,或者 1.1K
, 1.2K
, 1.9K
等,如果不是偶數千,否則如果小于千,顯示正常500
,100
,250
等,使用 JavaScript 格式化數字?
I need to show a currency value in the format of 1K
of equal to one thousand, or 1.1K
, 1.2K
, 1.9K
etc, if its not an even thousands, otherwise if under a thousand, display normal 500
, 100
, 250
etc, using JavaScript to format the number?
推薦答案
聽起來應該適合你:
function kFormatter(num) {
return Math.abs(num) > 999 ? Math.sign(num)*((Math.abs(num)/1000).toFixed(1)) + 'k' : Math.sign(num)*Math.abs(num)
}
console.log(kFormatter(1200)); // 1.2k
console.log(kFormatter(-1200)); // -1.2k
console.log(kFormatter(900)); // 900
console.log(kFormatter(-900)); // -900
這篇關于如果一千或更多,則將數字格式化為 2.5K,否則為 900的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!