Spring [removed]SpEL) - Rounding

后端 未结 1 545
无人共我
无人共我 2021-01-28 15:06

Is there a way to specify rounding precision when evaluating math operation using SpEL? For example

ExpressionParser parser  = new SpelExpressionParser();
Expre         


        
相关标签:
1条回答
  • 2021-01-28 15:27

    This works for me:

    "2.0/3"
    

    and result is 0.6666666666666666.

    Does it make sense?

    The point of the getValue(Double.class) is eliminated here since your 2/3 is just an operation between two integers and the result is an integer as well - 0. And only after that this result is converted to expected double as a 0.0.

    You need to explicitly say in the expression that you are going to deal with doubles.

    You may consider this as a limitation, but getValue(Double.class) is not a casting operation like in Java. It is a post-conversion. Therefore precision is just lost because your expression evaluates to integer anyway.

    0 讨论(0)
提交回复
热议问题