-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: input-number add error #1008
Conversation
This pull request is being automatically deployed with Vercel (learn more). 🔍 Inspect: https://vercel.com/tusimple/naive-ui/6rRYbY5p1kDh15mpeTeG4maYXUiA |
Codecov Report
@@ Coverage Diff @@
## main #1008 +/- ##
==========================================
+ Coverage 45.57% 47.07% +1.49%
==========================================
Files 509 511 +2
Lines 12478 12593 +115
Branches 3510 3548 +38
==========================================
+ Hits 5687 5928 +241
+ Misses 5789 5644 -145
- Partials 1002 1021 +19
Continue to review full report at Codecov.
|
src/input-number/src/InputNumber.tsx
Outdated
@@ -143,7 +143,7 @@ export default defineComponent({ | |||
return null | |||
} | |||
if (validator(parsedValue)) { | |||
let nextValue = parsedValue + offset | |||
let nextValue = parseFloat((parsedValue + offset).toFixed(10)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这么做是不准确的
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
我查了一下很多地方都是这个方法,你有啥更好的办法吗
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Talljack 我的思路是取parsedValue与offset值的精度最大值maxPrecision,然后Math.pow()进行精度的处理,最后toFixed(maxPrecision)这样......
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
在 slider 里面有人处理了类似的问题,可以参考一下
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok
@Talljack 测试安排 |
这种能找到原本实现 bug 的 case 都是很适合写单元测试的 |
@@ -143,7 +150,7 @@ export default defineComponent({ | |||
return null | |||
} | |||
if (validator(parsedValue)) { | |||
let nextValue = parsedValue + offset | |||
let nextValue = parseFloat((parsedValue + offset).toFixed(precisionRef.value)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1.005.toFixed(2)
返回 "1.00"
,看起来 toFixed
不是一个安全的解决方案
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1.005应该会取到最大的precisions,precisionRef.value会为3
* fix: input-number add error * fix: floating point decimal precision * fix: add test Co-authored-by: yugang.cao <yugang.cao@tusimple.ai>
close #1007