Skip to content
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

参数为字符串且末尾包含空格导致计算错误 #33

Open
Cinux-Chosan opened this issue Jun 8, 2020 · 3 comments
Open

参数为字符串且末尾包含空格导致计算错误 #33

Cinux-Chosan opened this issue Jun 8, 2020 · 3 comments

Comments

@Cinux-Chosan
Copy link

NP.times('0.1', 100); // 10
NP.times('0.1 ', 100); // 1

第一种情况是正常的计算,第二种情况计算结果异常,原因是 '0.1 ' 中末尾包含空格。

调试发现 NP 在计算参数小数位数之前没有将其先转换为数字再计算,因此空格也算在小数位数之中并且空格在进行运算的时候不会报错,因此导致 NP 静默错误。

NP 是否需要处理参数为字符串的情况?

@xiaosu12138
Copy link
Contributor

字符串输入运算已经支持了,我的个人理解啊,像这种本身输入就不是一个合法的数字字符串,是没必要考虑的,这种非法的字符串输入应该在运算前就限制住,保证我在运算的时候肯定是合法的输入,就像undefined、null和其他非法字符串一样本身就不应该参与运算

@tangshengfei
Copy link

tangshengfei commented Nov 1, 2021

确实库只是保证根据输入返回给你正确的结果,不应该干与计算无关的事情,这才叫纯粹的库。

1 similar comment
@AmandaYi
Copy link

AmandaYi commented Jan 6, 2022

确实库只是保证根据输入返回给你正确的结果,不应该干与计算无关的事情,这才叫纯粹的库。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants