6

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);
}
Chris Cashwell
  • 22,308
  • 13
  • 63
  • 94

4 Answers4

10

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.

Spikatrix
  • 20,225
  • 7
  • 37
  • 83
Jay
  • 18,959
  • 11
  • 53
  • 72
  • You'd better do a `"0000000000".substring(0, decimalCount)` (assuming `decimalCount` is between 1 and 10) than concatenations, performance-wise. – Thomas Broyer Aug 28 '11 at 10:48
  • 3
    IMO, the best approach is to combine the StringBuilder with a substring (if you need 24 "0"'s, then first append a 10-char-long string to the StringBuilder before appending a `substring(0, 4)`; or alternatively, append the 10-char-long string three times, and take a `substring(0, 24)` of the computed whole string). And if you need to do it very often in your app, try different variations and benchmark them! – Thomas Broyer Aug 29 '11 at 14:03
9

You can use

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

from GWT library which will render for example 1234.789789 to 1234.78

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

Andrei T
  • 511
  • 1
  • 10
  • 16
1

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

private final NumberFormat numberFormat = NumberFormat.getDecimalFormat();

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

Edit: added GWT versions per request

1

You can also specify the decimal format within the UiBinder file

<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>