格式化
数字
金额格式化
去除金额里小数点后无意义的0
,可使用parseFloat
方法
parseFloat('2.00') // 2
parseFloat('2.20') // 2.2
parseFloat('2.02') // 2.02
1
2
3
2
3
将数字转换成亿、万
function formatCount(count, precision = 1) {
if (count > 100000000) {
return parseFloat((count / 100000000).toFixed(precision)) + '亿';
}
if (count > 10000) {
return parseFloat((count / 10000).toFixed(precision)) + '万';
}
return count;
}
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
加parseFloat
的原因是,将2.0
转成2
。
字符串格式化
命名方法转换
- 小驼峰命名法,camelCase
- 大驼峰命名法,UpperCamelCase,也称为 Pascal 命名法。
- 中划线命名法,kebab-case
- 下划线命名法,snake_case
/**
* Camelize a hyphen-delimited string.
*/
const camelizeRE = /-(\w)/g
export const camelize = (str: string): string => {
return str.replace(camelizeRE, (_, c) => c ? c.toUpperCase() : '')
}
1
2
3
4
5
6
7
2
3
4
5
6
7
/**
* Hyphenate a camelCase string.
*/
const hyphenateRE = /\B([A-Z])/g
export const hyphenate = (str: string): string => {
return str.replace(hyphenateRE, '-$1').toLowerCase()
}
1
2
3
4
5
6
7
2
3
4
5
6
7