js千分位分隔,數字貨幣化方法學習記錄

2022-02-16 16:44:36 字數 3794 閱讀 3724

js千分位分隔,數字貨幣化—4種方法(含正則)

// 整數貨幣化

function intcurrency(num)

var numstring = parseint(num).tostring();

var len = numstring.length;

if (len < 3)

var n = len % 3;

if (n > 0) /g).join(",");

} else /g).join(",");}}

console.group("------------整數貨幣化")

console.log(intcurrency("abs"))

console.log(intcurrency("0"))

console.log(intcurrency("10"))

console.log(intcurrency("100"))

console.log(intcurrency("1000"))

console.log(intcurrency("10000"))

console.log(intcurrency("100000"))

console.log(intcurrency("1000000"))

console.log(intcurrency("10000000"))

console.log(intcurrency("100000000"))

console.log(intcurrency("1000000000"))

console.log(intcurrency("1000000000.0"))

console.log(intcurrency("1000000000.00"))

console.log(intcurrency("1000000000.000"))

// 整數部分

function intcurrency(num)

var len = num.length;

if (len < 3)

var n = len % 3;

if (n > 0) /g).join(",");

} else /g).join(",");}}

// 小數部分

function decimalcurrency(num)

var len = num.length; // 小數部分長度

if (len < 3)

var n = len % 3;

if (n > 0) /g).join(',') + ',' + num.slice(len - n);

} else /g).join(',')}}

// 貨幣化

function currency(num) else

return intcurrency(integerpart) + (decmialpart !== undefined ? ('.' + decimalcurrency(decmialpart)) : '');

}console.group("------------整數或小數貨幣化")

console.log(currency("0"))

console.log(currency("10"))

console.log(currency("100"))

console.log(currency("1000"))

console.log(currency("10000"))

console.log(currency("100000"))

console.log(currency("1000000"))

console.log(currency("10000000"))

console.log(currency("100000000"))

console.log(currency("1000000000"))

console.log(currency("1000000000.0"))

console.log(currency("1000000000.00"))

console.log(currency("1000000000.000"))

console.log(currency("1000000000.0000"))

console.log(currency("1000000000.00000"))

// 直接使用正則

function currency(num) )+(?!\d))/g, ',');

}console.group("------------取整數,正則貨幣化1")

console.log(currency("0"))

console.log(currency("10"))

console.log(currency("100"))

console.log(currency("1000"))

console.log(currency("10000"))

console.log(currency("100000"))

console.log(currency("1000000"))

console.log(currency("10000000"))

console.log(currency("100000000"))

console.log(currency("1000000000"))

console.log(currency("1000000000.0"))

console.log(currency("1000000000.00"))

console.log(currency("1000000000.000"))

console.log(currency("1000000000.0000"))

console.log(currency("1000000000.00000"))

// 直接使用正則

function currency(num) )+$)/g, ',');

}console.group("------------取整數,正則貨幣化2")

console.log(currency("0"))

console.log(currency("10"))

console.log(currency("100"))

console.log(currency("1000"))

console.log(currency("10000"))

console.log(currency("100000"))

console.log(currency("1000000"))

console.log(currency("10000000"))

console.log(currency("100000000"))

console.log(currency("1000000000"))

console.log(currency("1000000000.0"))

console.log(currency("1000000000.00"))

console.log(currency("1000000000.000"))

console.log(currency("1000000000.0000"))

console.log(currency("1000000000.00000"))

正則校驗

// 正整數或小數 /^[0-9]+(\.[0-9]+)?$/g

// 正、負整數或小數,也可不輸入正負 /^(\+|-)?\d+(\.\d+)?$/g

**於:

給數字新增千分位分隔符

法一 int i 0,j 0 void recombination char a,long long n if n 10 0 include intmain recombination a,n for k j k 0 k printf c a k return0 注意 此程式可在educoder中實...

js演算法實現千分位分隔符

var str 123456789 9,8,7,6,5,4,3,2,1 function formatcash str console.log formatcash str 複製 另外一種優化演算法,因為字串在 js 每次變化都會重新開闢記憶體,所有轉化為陣列會更加便捷 function f str...

js 千分位顯示方法

問題描述 應用中金錢數目常常使用千分位分隔,使用js寫了乙個簡單的函式。解決方案 可以使用字串操作,也可以陣列操作,不過最簡單的還是正則替換。參考方案 1.假設輸入為1234567.89,正則中先提取出數額的整數部分n 1234567 2.然後對n繼續使用正則替換,d 表示要查詢乙個數字,是正向肯定...