本文介绍了通过java.text.DecimalFormat格式化数字始终在SSJS中返回错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用SSJS中的 java.text.DecimalFormat 格式化数字,但它会返回错误。这是我的代码片段。

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.DecimalFormat = new java.text.DecimalFormat("000");
df.format(50);

这会返回错误 调用格式时的歧义(长)和格式(双) 。所以我试图将数字解析为double或long但仍然是相同的错误。

This returns an error of Ambiguity when calling format(long) and format(double). So I tried to parse the number as double or long but still the same error.

df.format(java.lang.Long.parseLong("50"));     //Returns same error
df.format(java.lang.Double.parseDouble("50")); //Returns same error

我创建了上述SSJS代码的Java实现,它工作正常。 / p>

I created a Java implementation of the above SSJS code and it works fine.

DecimalFormat df = new DecimalFormat("000"); 
return df.format(50);

我有很多SSJS代码(上面的代码片段是其中的一部分)和创建两行的新Java类似乎太费力了。任何人都知道为什么这在SSJS中不起作用?

I have quite a few lines of SSJS code (of which the above snippet is part of) and creating a new Java class for two lines seems too much effort. Anyone knows why this doesn't work in SSJS?

推荐答案

@Naveen:您是否尝试过以下代码,它对我有用而无需创建任何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 ;)

这篇关于通过java.text.DecimalFormat格式化数字始终在SSJS中返回错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 00:34