Formatting number via java.text.DecimalFormat always returns error in SSJS

后端 未结 2 1163
無奈伤痛
無奈伤痛 2020-12-21 07:19

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         


        
相关标签:
2条回答
  • 2020-12-21 07:53

    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.

    0 讨论(0)
  • 2020-12-21 07:58

    @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 ;)

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