限制GWT中的小数位数? [英] Limiting Number of Decimal Places in GWT?

查看:127
本文介绍了限制GWT中的小数位数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在纯Java中,我通常会有一个像下面这样的函数,用于将给定数字值的小数位数限制为 decimalCount 。但是,根据GWT文档,GWT不提供日期和数字格式化类的完全仿真(例如java.text.DateFormat,java.text.DecimalFormat,java.text.NumberFormat和java.TimeFormat)。为了使它在GWT中工作,人们会做什么?

  public static String getFormatted(double value,int decimalCount){
DecimalFormat decimalFormat = new DecimalFormat();
decimalFormat.setMaximumFractionDigits(decimalCount);
返回decimalFormat.format(value);


解决方案

查看 NumberFormat (com。 GWT Javadoc。

我从来没有用过它,但我看到这个例子:

  //自定义格式
value = 12345.6789;
formatted = NumberFormat.getFormat(000000.000000)。format(value);
//在默认语言环境
中打印012345.678900 GWT.log(格式化的字符串是格式化的);

所以这应该适合您。



更新



此方法提供了与问题中相同的功能。我继续前行,并要求提供最有效的方法来解决这个问题,请参阅此处。 (对不起,这个答案已经被编辑了很多 - 它只是在窃听我)

  public static String getFormatted(double value,int decimalCount ){
StringBuilder numberPattern = new StringBuilder(
(decimalCount< = 0)?:。);
for(int i = 0; i< decimalCount; i ++){
numberPattern.append('0');
}
return NumberFormat.getFormat(numberPattern.toString())。format(value);
}

其他选项包括使用一定数量的0并使用子字符串把@Thomas Broyer在评论中提到的所需模式拉出来。


In pure Java, I would normally have a function like the one below for limiting the number of decimal places to decimalCount for a given number value. However, according to the GWT docs, "GWT does not provide full emulation for the date and number formatting classes (such as java.text.DateFormat, java.text.DecimalFormat, java.text.NumberFormat, and java.TimeFormat)." What would one do to the following function in order to make it work in GWT?

public static String getFormatted(double value, int decimalCount) { 
    DecimalFormat decimalFormat = new DecimalFormat();
    decimalFormat.setMaximumFractionDigits(decimalCount);
    return decimalFormat.format(value);
}

解决方案

Check out NumberFormat (com.google.gwt.i18n.client.NumberFormat) in the GWT Javadoc.

I've never used it but I see this example in there:

// Custom format
value = 12345.6789;
formatted = NumberFormat.getFormat("000000.000000").format(value);
// prints 012345.678900 in the default locale
GWT.log("Formatted string is" + formatted);

So this should work for you.

Update

This method provides the same functionality as the one in your question. I went ahead and asked for the most efficient way to go about this, see that question here. (Sorry this answer has been edited so much - it was just bugging me)

public static String getFormatted(double value, int decimalCount) {
    StringBuilder numberPattern = new StringBuilder(
            (decimalCount <= 0) ? "" : ".");
    for (int i = 0; i < decimalCount; i++) {
        numberPattern.append('0');
    }
    return NumberFormat.getFormat(numberPattern.toString()).format(value);
}

Alternatives include using a set amount of "0"'s and using substring to pull out the required pattern as @Thomas Broyer mentioned in the comments.

这篇关于限制GWT中的小数位数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆