Show only two digit after decimal [duplicate] Show only two digit after decimal [duplicate] java java

Show only two digit after decimal [duplicate]


Use DecimalFormat.

DecimalFormat is a concrete subclass of NumberFormat that formats decimal numbers. It has a variety of features designed to make it possible to parse and format numbers in any locale, including support for Western, Arabic, and Indic digits. It also supports different kinds of numbers, including integers (123), fixed-point numbers (123.4), scientific notation (1.23E4), percentages (12%), and currency amounts ($123). All of these can be localized.

Code snippet -

double i2=i/60000;tv.setText(new DecimalFormat("##.##").format(i2));

Output -

5.81


How about String.format("%.2f", i2)?


Here i will demonstrate you that how to make your decimal number short. Here i am going to make it short upto 4 value after decimal.

  double value = 12.3457652133  value =Double.parseDouble(new DecimalFormat("##.####").format(value));