topic
Format thousands of digits, for example:
10201004050 is converted to 10,201,004,050
train of thought
Note that it is a reverse order judgment, and a comma is added for every 3 digits.
Idea 1: Convert to an array, perform reverse traversal, and split every 3 digits
Idea 2: Convert to a string, traverse in reverse order, and split every 3 digits
Idea 3: Regular (poor performance)
Performance:
number > string > array > regular
the code
/**
* 千分位格式化(使用数组)
* @param n number
*/
export function format1(n: number): string {
n = Math.floor(n) // 只考虑整数
const s = n.toString()
const arr = s.split('').reverse()
return arr.reduce((prev, val, index) => {
if (index % 3 === 0) {
if (prev) {
return val + ',' + prev
} else {
return val
}
} else {
return val + prev
}
}, '')
}
/**
* 数字千分位格式化(字符串分析)
* @param n number
*/
export function format2(n: number): string {
n = Math.floor(n) // 只考虑整数
let res = ''
const s = n.toString()
const length = s.length
for (let i = length - 1; i >= 0; i--) {
const j = length - i
if (j % 3 === 0) {
if (i === 0) {
res = s[i] + res
} else {
res = ',' + s[i] + res
}
} else {
res = s[i] + res
}
}
return res
}
// 功能测试
const n = 10201004050
console.info('format1', format1(n))
console.info('format2', format2(n))