gpt4 book ai didi

将百分比添加到非百分比的 Javascript 解析器

转载 作者:行者123 更新时间:2023-11-30 20:19:40 24 4
gpt4 key购买 nike

寻找一种一致的方式来编写(或扩展)解析器来计算数学方程式。我正在使用 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))

您也可以执行相同的操作,但扩展 ma​​thjs 库执行 math.plusPecentage = function () {//code}

关于将百分比添加到非百分比的 Javascript 解析器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51584858/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com