完善 code review 提到的问题

This commit is contained in:
puhui999
2023-08-23 15:04:46 +08:00
parent 55772cbba0
commit 7db6c121bb
16 changed files with 184 additions and 189 deletions

View File

@ -174,7 +174,6 @@ export const copyValueToTarget = (target, source) => {
Object.assign(target, newObj)
}
// TODO @puhui999返回要带上 .00 哈.例如说 1.00
/**
* 将一个整数转换为分数保留两位小数
* @param num
@ -185,6 +184,31 @@ export const formatToFraction = (num: number | string | undefined): number => {
return parseFloat((parsedNumber / 100).toFixed(2))
}
/**
* 将一个数转换为 1.00 这样
* 数据呈现的时候使用
*
* @param num 整数
*/
export const floatToFixed2 = (num: number | string | undefined): string => {
let str = '0.00'
if (typeof num === 'undefined') {
return str
}
const f = formatToFraction(num)
const decimalPart = f.toString().split('.')[1]
const len = decimalPart ? decimalPart.length : 0
switch (len) {
case 0:
str = f.toString() + '.00'
break
case 1:
str = f.toString() + '0'
break
}
return str
}
/**
* 将一个分数转换为整数
* @param num