寻找一种一致的方式来编写(或扩展)解析器以执行数学方程式。我大部分使用mathjs,效果很好。但是它不处理百分比。我正在尝试编写规则,以处理将百分比添加到百分比,将百分比添加到非百分比等。
1 + 6 + 7%= 7.49 –正确。
1 + 6 + 7%= 7.07 –错误。
56.47 + 15%= 64.9405 –正确。
56.47 + 15%= 56.62-错误。
等。
欢迎提供任何提示或建议。
答案 0 :(得分:1)
您可以执行以下操作:
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))
您也可以执行相同的操作,但是扩展{strong> mathjs 库,方法是math.plusPecentage = function () {//code}