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

能不能像jQuery那样链式操作呢 #36

Open
Gaooly opened this issue Jul 9, 2020 · 5 comments
Open

能不能像jQuery那样链式操作呢 #36

Gaooly opened this issue Jul 9, 2020 · 5 comments

Comments

@Gaooly
Copy link

Gaooly commented Jul 9, 2020

期望

NP.plus(0.1, 0.2).times(3)// = 0.9    not 0.9000000000000001
@yungsun09
Copy link

期望

NP.plus(0.1, 0.2).times(3)// = 0.9    not 0.9000000000000001

https://github.com/yungsun09/number-precision-chainable 你可以看下效果,但真用起来感觉会死(x

@defims
Copy link

defims commented Dec 26, 2020

@yungsun09 number-precision-chainable 链式调用很棒。
我理解@gaolingyi-hadi 是希望使用的方法更易读,这里安利下 number-precision-macro,支持NPM((1+1)/2)这样的js表达式写法,而不用使用NP.divide(NP.plus(1, 1),2)

@ruoru
Copy link

ruoru commented Jul 9, 2021

建议官方采纳一下 defims/number-precision-macro

@yungsun09
Copy link

@yungsun09 number-precision-chainable 链式调用很棒。
我理解@gaolingyi-hadi 是希望使用的方法更易读,这里安利下 number-precision-macro,支持NPM((1+1)/2)这样的js表达式写法,而不用使用NP.divide(NP.plus(1, 1))

你这个真的很厉害了,我都没想过还能这么写,star了

@chenning111
Copy link

Number.prototype.plus = function (arg) {
return NP.plus(this, arg)
}
// 减法
Number.prototype.minus = function (arg) {
return NP.minus(this, arg)
}
// 乘法
Number.prototype.times = function (arg) {
return NP.times(this, arg)
}
// 除法
Number.prototype.divide = function (arg) {
return NP.divide(this, arg)
}
// 四舍五入
Number.prototype.round = function (arg) {
return NP.round(this, arg)
}
可以在原型上面增加方法 这样就支持了,但是计算只能是一个参数 例如:NP.plus(0.1, 0.2).times(3),如果是NP.plus(0.1, 0.2).times(3,2), 再除以2无效

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

5 participants