寻找一种一致的方法来编写(或扩展)一个解析器来完成数学方程。我在用 mathjs 在大多数情况下,它工作得很好。但它不处理百分比。我正在尝试编写一些规则来处理将百分比添加到百分比、将百分比添加到非百分比等。
1+6+7%=7.49正确。 1+6+7%=7.07不正确。
56.47+15%=64.9405正确。 56.47+15%=56.62不正确。
等。。
欢迎提供任何建议或建议。
你可以这样做:
Number.prototype.plusPecentage = function(n){ return this+(this*n/100); } console.log((1 + 6).plusPecentage(7))
或者:
Math.plusPecentage = function(n,p){ return n+(n*p/100); } console.log(Math.plusPecentage(7,7))
你也可以做同样的事情,但是要扩展 马蒂斯 图书馆做 math.plusPecentage = function () {//code}
math.plusPecentage = function () {//code}