简体   繁体   中英

Is it possible to divide 2 variables in .properties file

How to divide two different variables in JAVA .properties file

When I do like this:

amount = 10
cur = 5
sum=${amount}/${cur} 

I get sum as a String:

10/5

You could do that in few ways:

after receiving an value from the properties file, you can evaluate it in for example built in javascript engine since java 1.6:

import javax.script.ScriptEngineManager;
import javax.script.ScriptEngine;

public class ScriptEngineTest {
  public static void main(String[] args) throws Exception{
    ScriptEngineManager mgr = new ScriptEngineManager();
    ScriptEngine engine = mgr.getEngineByName("JavaScript");
    String value = "10/5";
    System.out.println(engine.eval(value));
    } 
}

or you can write your own evaluator/ use different library.

You can even extend an ResourceBundle, and override the get method to eval each value before you get it if that is desired.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM