I am trying to format a number using java.text.DecimalFormat
in SSJS but it returns an error. Here is my code snippet.
var df:java.text.DecimalF
It does not work because SSJS cannot tell the difference between double and float (this is by design - javascript has only concept of number with 64 bit precision).
You can probably hack this with reflection:
var df:java.text.DecimalFormat = new java.text.DecimalFormat("000");
df.getClass().getMethod("format", Double.class).invoke(df, 50);
But I would rather create some custom java utils classes for this purpose. SSJS is awful for almost anything except for calling java.
@Naveen: Have you tried below code, it worked for me without creating any java class :)
var df:java.text.DecimalFormat = new java.text.DecimalFormat("000");
var dblNum: java.lang.Double = new java.lang.Double(50);
return df.format(dblNum);
Thanks, please let me know your comments on this ;)