Format a number with leading sign

后端 未结 4 1719
耶瑟儿~
耶瑟儿~ 2020-12-05 06:39

How do I format in Java a number with its leading sign?

Negative numbers are correctly displayed with leading -, but obviously positive numbers are not

4条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-05 06:43

    API for Formatter provides an example:

    Formatter formatter = new Formatter();
    System.out.println(formatter.format(Locale.FRANCE, "e = %+10.4f", Math.E));
    //e =    +2,7183
    

提交回复
热议问题