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

后端 未结 2 1189
無奈伤痛
無奈伤痛 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.

提交回复
热议问题