Math.js Error: Cannot convert "abc" to a number
Math.js Error: Cannot convert "abc" to a number
我在 React 中有一个 math.js 函数。它所做的是使用变量中的值评估公式,例如:
var variables = {'A': 1, 'B': 2}
var formula = 'A > 0 or B < 5'
math.evaluate(formula, variables) // this will return true
但是比较字符串时,returns出现如下错误,
Error: Cannot convert "abc" to a number
var variables = {'A': 'abc'}
var formula = 'A == "abc"'
math.evaluate(formula, variables) // this will return Error: Cannot convert "abc" to a number
谁能指导我如何操作?我需要的是变量可以对字符串和数字进行评估,谢谢。任何与此类似的问题也将不胜感激。我试图搜索,但搜索不多。
我试过以下方法,它没有覆盖函数:
https://github.com/josdejong/mathjs/issues/1051#issuecomment-369930811
更新
这就是我想要做的:
formula = ' A >= 2 and B == "year" '
variables = { A: 23, B: 'year' }
import { evaluate } from 'mathjs'
const MATH_EVAL = (formula, variables)=> {
var result = evaluate(formula, variables)
return result
}
export default MATH_EVAL
我有点困惑,因为这可以使用问题评论吗?可能是打字错误?
var variables = {'A': 'abc'}
var formula = 'A == "abc"'
math.import({
equal: function (a, b) { return a === b }
}, {override: true})
console.log(math.evaluate(formula, variables))
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjs/6.0.2/math.js"></script>
https://github.com/josdejong/mathjs/issues/1557
上述问题的答案就在上面提到的link上。它说,我引用:
I've created an example for this: examples/advanced/custom_relational_functions.js. Can you have a look at that and see if it makes sense and solves your issue?
The main difference is that the created functions don't update their dependencies anymore when you replace an existing function with a new one. I have to give that some more thought, maybe we can make this work when working with a mathjs instance (instead of pure functions).
我在 React 中有一个 math.js 函数。它所做的是使用变量中的值评估公式,例如:
var variables = {'A': 1, 'B': 2}
var formula = 'A > 0 or B < 5'
math.evaluate(formula, variables) // this will return true
但是比较字符串时,returns出现如下错误,
Error: Cannot convert "abc" to a number
var variables = {'A': 'abc'}
var formula = 'A == "abc"'
math.evaluate(formula, variables) // this will return Error: Cannot convert "abc" to a number
谁能指导我如何操作?我需要的是变量可以对字符串和数字进行评估,谢谢。任何与此类似的问题也将不胜感激。我试图搜索,但搜索不多。
我试过以下方法,它没有覆盖函数: https://github.com/josdejong/mathjs/issues/1051#issuecomment-369930811
更新 这就是我想要做的:
formula = ' A >= 2 and B == "year" '
variables = { A: 23, B: 'year' }
import { evaluate } from 'mathjs'
const MATH_EVAL = (formula, variables)=> {
var result = evaluate(formula, variables)
return result
}
export default MATH_EVAL
我有点困惑,因为这可以使用问题评论吗?可能是打字错误?
var variables = {'A': 'abc'}
var formula = 'A == "abc"'
math.import({
equal: function (a, b) { return a === b }
}, {override: true})
console.log(math.evaluate(formula, variables))
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjs/6.0.2/math.js"></script>
https://github.com/josdejong/mathjs/issues/1557
上述问题的答案就在上面提到的link上。它说,我引用:
I've created an example for this: examples/advanced/custom_relational_functions.js. Can you have a look at that and see if it makes sense and solves your issue?
The main difference is that the created functions don't update their dependencies anymore when you replace an existing function with a new one. I have to give that some more thought, maybe we can make this work when working with a mathjs instance (instead of pure functions).