Skip to content

Commit

Permalink
fix: support for negative numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
cn-xufei committed Dec 29, 2023
1 parent 13631b4 commit e3f8ce5
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions packages/shared/src/number.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,16 @@ export function calcTimeUnit(val, fix = 2, op) {
}
]
let results = []
let prefix = ''
if (val < 0) {
val = Math.abs(val)
prefix = '-'
}
if (typeof val !== 'number' || val === 0) {
return '0'
} else if (val > 0 && val < 1) {
const p = Math.pow(10, options.digits)
return Math.ceil(val * p) / p + units[0].unit
return prefix + Math.ceil(val * p) / p + units[0].unit
}
const ts = Math.floor(val)

Expand All @@ -141,7 +146,10 @@ export function calcTimeUnit(val, fix = 2, op) {
results = results.slice(0, fix)
}

return results.reduce((pre, current) => {
return pre + (pre ? options.separator : '') + current.value + current.util
}, '')
return (
prefix +
results.reduce((pre, current) => {
return pre + (pre ? options.separator : '') + current.value + current.util
}, '')
)
}

0 comments on commit e3f8ce5

Please sign in to comment.