java 限制 GWT 中的小数位数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7214377/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 19:07:28  来源:igfitidea点击:

Limiting Number of Decimal Places in GWT?

javamathgwtnumerical-methods

提问by Chris Cashwell

In pure Java, I would normally have a function like the one below for limiting the number of decimal places to decimalCountfor 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?

在纯 Java 中,我通常会有一个类似于下面的函数,用于限制decimalCount给定 number的小数位数value。但是,根据 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);
    return decimalFormat.format(value);
}

回答by JayQuerie.com

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

查看GWT Javadoc 中的NumberFormat(com.google.gwt.i18n.client.NumberFormat)。

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.

替代方案包括使用一定数量的“0”并使用子字符串提取所需的模式,如评论中提到的@Thomas Broyer。

回答by Andrei T

You can use

您可以使用

NumberFormat decimalFormat = NumberFormat.getFormat(".##");

from GWT library which will render for example 1234.789789 to 1234.78

来自 GWT 库,它将呈现例如 1234.789789 到 1234.78

You can find a full working example here: http://gwt.google.com/samples/Showcase/Showcase.html#!CwNumberFormat

您可以在此处找到完整的工作示例:http: //gwt.google.com/samples/Showcase/Showcase.html#!CwNumberFormat

回答by user3561002

In current GWT (2.5, 2.6), there is now:

在当前的 GWT (2.5, 2.6) 中,现在有:

private final NumberFormat numberFormat = NumberFormat.getDecimalFormat();

System.out.println(numberFormat.format(myFloat));

Edit: added GWT versions per request

编辑:每个请求添加 GWT 版本

回答by darkhorse21

You can also specify the decimal format within the UiBinder file

您还可以在 UiBinder 文件中指定十进制格式

<g:HTMLPanel ui:field="grid">
  <table>
    <tr>
      <td>
        <g:Label>
            <ui:msg description="total">Total:</ui:msg>
        </g:Label>
      </td>
      <td>
        <g:NumberLabel customFormat="0.00" ui:field="total"/>
      </td>
    </tr>
  </table>
</g:HTMLPanel>