Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #19365

Re: Operation in String to Double conversion

From "John B. Matthews" <nospam@nospam.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: Operation in String to Double conversion
Date 2012-10-15 13:19 -0400
Organization The Wasteland
Message-ID <nospam-64C7D2.13194715102012@news.aioe.org> (permalink)
References <3a63483c-4322-4bfb-8c28-2d528bf48443@googlegroups.com>

Show all headers | View raw


In article <3a63483c-4322-4bfb-8c28-2d528bf48443@googlegroups.com>,
 William Lopes <williamlopes.dev@gmail.com> wrote:

> I have to do a conversion between String and Double object, but my 
> string is a mathematical operation like "100 + 10". Even when I make 
> a conversion using NumberFormat.getInstance of "100 + 10", my result 
> is 100.0 only.
> 
> I would like to do it without split my string of way manually.

Your distribution may already contain a suitable implementation of 
javax.script.ScriptEngine:

    ScriptEngineManager mgr = new ScriptEngineManager();
    List<ScriptEngineFactory> factories = mgr.getEngineFactories();
    for (ScriptEngineFactory f : factories) {
        System.out.println(f);
    }

Selecting the available RhinoScriptEngine by extension

    ScriptEngine engine = mgr.getEngineByExtension("js");
    try {
        System.out.println(engine.eval("5 * 8 + 2"));
    } catch (ScriptException ex) {
        ex.printStackTrace(System.err);
    }

prints the expected answer, 42.0.

-- 
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Operation in String to Double conversion William Lopes <williamlopes.dev@gmail.com> - 2012-10-14 12:39 -0700
  Re: Operation in String to Double conversion Martin Gregorie <martin@address-in-sig.invalid> - 2012-10-14 20:32 +0000
    Re: Operation in String to Double conversion William Lopes <williamlopes.dev@gmail.com> - 2012-10-14 14:08 -0700
      Re: Operation in String to Double conversion markspace <-@.> - 2012-10-14 15:59 -0700
        Re: Operation in String to Double conversion William Lopes <williamlopes.dev@gmail.com> - 2012-10-14 17:45 -0700
        Re: Operation in String to Double conversion frank.asseg@gmail.com - 2012-10-15 11:18 -0700
          Re: Operation in String to Double conversion Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2012-10-16 00:14 +0200
            Re: Operation in String to Double conversion frank.asseg@gmail.com - 2012-10-15 15:41 -0700
        Re: Operation in String to Double conversion frank.asseg@gmail.com - 2012-10-15 11:27 -0700
          Re: Operation in String to Double conversion Lew <lewbloch@gmail.com> - 2012-10-15 12:40 -0700
            Re: Operation in String to Double conversion frank.asseg@gmail.com - 2012-10-15 14:24 -0700
              Re: Operation in String to Double conversion Lew <lewbloch@gmail.com> - 2012-10-15 14:48 -0700
              Re: Operation in String to Double conversion Arved Sandstrom <asandstrom2@eastlink.ca> - 2012-10-15 19:14 -0300
                Re: Operation in String to Double conversion Arne Vajhoej <arne@vajhoej.dk> - 2012-10-17 16:58 -0400
              Re: Operation in String to Double conversion Arne Vajhoej <arne@vajhoej.dk> - 2012-10-17 17:02 -0400
            Re: Operation in String to Double conversion Arne Vajhoej <arne@vajhoej.dk> - 2012-10-17 17:13 -0400
              Re: Operation in String to Double conversion Eric Sosman <esosman@comcast-dot-net.invalid> - 2012-10-17 17:32 -0400
  Re: Operation in String to Double conversion Roedy Green <see_website@mindprod.com.invalid> - 2012-10-14 22:49 -0700
  Re: Operation in String to Double conversion "John B. Matthews" <nospam@nospam.invalid> - 2012-10-15 13:19 -0400
    Re: Operation in String to Double conversion Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2012-10-15 12:51 -0700
      Re: Operation in String to Double conversion "John B. Matthews" <nospam@nospam.invalid> - 2012-10-16 09:39 -0400
  Re: Operation in String to Double conversion Eric Sosman <esosman@comcast-dot-net.invalid> - 2012-10-15 20:47 -0400
  Re: Operation in String to Double conversion Arne Vajhoej <arne@vajhoej.dk> - 2012-10-17 16:54 -0400

csiph-web