gpt4 book ai didi

android - EXP4J 是否处理小数

转载 作者:行者123 更新时间:2023-11-29 21:15:02 25 4
gpt4 key购买 nike

我的应用程序中有这个;

Calculable result= new ExpressionBuilder(s1).build();
size.setText(Double.toString(result.calculate()));
size_num = result.calculate();

如果我在 S1 中输入 100*.25,它会给我一个异常(exception)。没有小数点就好了。是否有 EXP4J 可以处理小数的解决方法?

此外,如果我在公式后输入制表符,EXP4J 似乎无法处理。有谁知道在调用 EXP4J 之前如何处理等式中的制表符?

谢谢!!

最佳答案

不是那些小数。

通过查看 the source of the tokenizer :

// ... snip ...

for (int i = 0; i < chars.length; i++) {

// ... snip ...

if (Character.isDigit(c)) { // matches digit

// ... snip ...

while (chars.length > i + numberLen) {
if (isDigitOrDecimalSeparator(chars[i + numberLen])) { // matches '.'

// ... snip ...

您可以看到,只有在标记中可以匹配小数点分隔符时,才会先匹配数字。

所以我怀疑这会很好:"100*0.25",但不是 "100*.25"

Also if I enter a tab after my equation, EXP4J can't seem to handle that. Does anyone know how to handle a tab in the equation before EXP4J is called?

tokenizer 忽略空格,所以要么用空格替换制表符,要么在解析之前一起删除制表符应该可以解决问题:

s1 = s1.replace("\t", "");
Calculable result= new ExpressionBuilder(s1).build();

关于android - EXP4J 是否处理小数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21649949/

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