понедельник, 21 июня 2010 г.

String.format depends on locality...

Неожиданно. Сужает область применения String.format до вывода в консоль.
public class xxx {
    public void xxxx() {
        double xxx = 123.5657;
        System.out.println(String.format("%.2f", xxx));//работет прально если Formatter formatter = new Formatter(sb, Locale.US)
        System.out.println((new BigDecimal(xxx)).setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString());
        String.format("%.2f", xxx).replaceAll(",", ".");

2 комментария:

  1. Can't read russian enough to literally understand this blog, but I think I get it from the listing. For the last line, just remember that even String.format() can accept a Locale, so it can be done with Locale.US without need for replaceAll().

  2. yep, 'replaceAll' is a kind of bullshit design =) that should never be used cause thers some other formating literals, for example space