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

String.format depends on locality...

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

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

    ОтветитьУдалить