Search code examples
javascriptjava-8nashorn

.toLocaleString in Nashorn engine


I've got this test code snippet:

ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
Object o = engine.eval("var i = 1000; i.toLocaleString('de-DE', { style: 'currency', currency: 'EUR' });");
System.out.println(o); 

When I start the test, I get this result :

1000

So, could it be, that Nashorn can't execute the .toLocaleString() function? Or must I do some extra work to get it run? I've searched in google, but didn't find an answer ...


Solution

  • Looking at the source code it seems that it doesn't support the toLocaleString function. When you call the function Nashorn redirects the call on jdk.nashorn.internal.objects.NativeNumber.toLocaleString(Object). This method simply transform the input object (that is a number) in a string representation. Here is the code:

    public static String toLocaleString(Object self) {
      return JSType.toString(getNumberValue(self));
    }
    

    This is the version I've just looked at:

    C:...\java\bin>jjs -version

    nashorn 1.8.0_121