GWTでString.format("%.2f",value)をしたい

概要

JavaでDoubleの桁数を制限してStringに変換するときは

//double値を小数点2桁でStringに変換
String.format(“%.2f”,value)

とするが、GWTJRE EmurationはString.format()に対応していない
参考:http://www.gwtproject.org/doc/latest/RefJreEmulation.html

そこで、NumberFormatクラスで代替する。
参考:http://stackoverflow.com/questions/7214377/limiting-number-of-decimal-places-in-gwt
NumberFormatの仕様:
http://www.gwtproject.org/javadoc/latest/com/google/gwt/i18n/client/NumberFormat.html


サンプル

*.java

    public static String getFormatted(double value) {
        NumberFormat decimalFormat = NumberFormat.getFormat(".##");
        return decimalFormat.format(value);
    }