Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use NumberFormat or DecimalFormat

Tags:

java

In most case, how can we justify, when shall we use

NumberFormat.getInstance();

When shall we use

new DecimalForamt(...);
like image 831
Cheok Yan Cheng Avatar asked Feb 19 '10 14:02

Cheok Yan Cheng


2 Answers

If you want to specify how your numbers are formatted, then you must use the DecimalFormat constructor. If you want "the way most numbers are represented in my current locale", then use the default instance of NumberFormat.

like image 100
Jonathan Feinberg Avatar answered Oct 12 '22 22:10

Jonathan Feinberg


NumberFormat.getInstance actually gets the formatter based on the locale and number style. It might actually return the DecimalFormat() object. "DecimalFormat is a concrete subclass of NumberFormat that formats decimal numbers" - From JDK javadocs

like image 42
Kannan Ekanath Avatar answered Oct 12 '22 23:10

Kannan Ekanath