Repository navigation
fix(format): 舍入到 0 的负数不再显示「−0.00%」 - #25
Merged
Merged
Conversation
-0.001 舍入后是 0.00,原来却打上减号,显示成「−0.00%」。现在符号跟着显示出来的数字走,舍入到 0 时统一用 +。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
问题
formatPercent先用原始值决定符号,再对绝对值做toFixed:-0.001舍入后是0.00,却带着减号,显示成−0.00%—— 符号和数字自相矛盾。行情里四舍五入到 0 的微跌很常见(两位小数下-0.004就是)。做法
body = Math.abs(value).toFixed(digits);body不为 0 时才用真减号;其余统一用+。正负两侧的进位与符号因此保持一致(
-0.006→−0.01%,-0.001→+0.00%)。测试
app/src/lib/__tests__/format.test.ts补了两组用例:舍入到 0 的负数(-0.001/-0.004/-0.04)与进位边界(0.004/-0.006/-1.005)。原有用例(+3.85%/−0.50%/+0.00%)保持通过。验证
cd app && npx vitest run src/lib/__tests__/format.test.ts→ 11 passedcd app && npm test→ 172 passedcd app && npm run lint→ 无输出