Check the NumberFormat (com.google.gwt.i18n.client.NumberFormat) in the GWT Javadoc.
I have never used it, but I see this example:
// 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 in your question. I went ahead and asked for the most efficient way, see this question here . (Sorry this answer has been edited so much - it just listened to 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 the specified amount of "0" and using a substring to extend the desired pattern as the @Thomas Broyer mentioned in the comments.
source share