102

How to get the double value that is only two digit after decimal point.

for example

if

i=348842.
double i2=i/60000;
tv.setText(String.valueOf(i2));

this code generating 5.81403333.

But I want only 5.81.

So what shoud I do?

Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103
URAndroid
  • 6,177
  • 6
  • 30
  • 40
  • 5
    This question is not really a duplicate of http://stackoverflow.com/questions/7415733/how-to-convert-double-to-2-number-after-the-dot This question is asking about formatting; that question is asking about changing the value in a `double`. – Raedwald Apr 03 '14 at 11:59

7 Answers7

201

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

Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103
153

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

bluish
  • 26,356
  • 27
  • 122
  • 180
dumazy
  • 13,857
  • 12
  • 66
  • 113
30

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));
Pir Fahim Shah
  • 10,505
  • 1
  • 82
  • 81
  • for set text to Textview Eg: double value = 12.3457652133 textview.setText( new DecimalFormat("##.##").format(Double.parseDouble(value))); – Digvijay Machale Jun 12 '17 at 06:49
  • 1
    This have a limitation, you should be sure that the local used by `DecimalFormat` is returning decimal value with a dot not comma, otherwise the `parseDouble` will throw `NumberFormatException` – Adil Feb 19 '18 at 13:37
  • 2
    Working good without error – Satyam Gondhale Apr 23 '20 at 13:18
17

Many other answers only do formatting. This approach will return value instead of only print format.

double number1 = 10.123456;
double number2 = (int)(Math.round(number1 * 100))/100.0;
System.out.println(number2);
Prashant Ghimire
  • 4,890
  • 3
  • 35
  • 46
9

I think the best and simplest solution is (KISS):

double i = 348842;
double i2 = i/60000;
float k = (float) Math.round(i2 * 100) / 100;
Sergii Stotskyi
  • 5,134
  • 1
  • 22
  • 21
  • 1
    I believe he's interested how to *display* only 2 significant digits. Also, because of the way floating point works, your solution will not result exactly in `5.81`, but something like `5.81000000001` or the like. – Petriborg Jun 09 '12 at 14:51
  • 3
    I think it's impossible! Because Math.round - returns integer and we cast it to float value. How is float value (581.0) after dividing by 100 can be something like 5.81000000001? Do you have test case that describe your opinion? – Sergii Stotskyi Jun 09 '12 at 15:27
  • May be this is a simple code for a programmer, It helped me in the critical situation. Thanks buddy...1 upvote for u – nmkyuppie Oct 16 '13 at 09:37
  • 1
    This method will be very prone to floating point math errors as @Petriborg pointed out – Ron Jensen Mar 29 '16 at 18:28
3
i=348842.
double i2=i/60000;
DecimalFormat dtime = new DecimalFormat("#.##"); 
i2= Double.valueOf(dtime.format(time));
v.setText(String.valueOf(i2));
MAC
  • 15,799
  • 8
  • 54
  • 95
3

First thing that should pop in a developer head while formatting a number into char sequence should be care of such details like do it will be possible to reverse the operation.

And other aspect is providing proper result. So you want to truncate the number or round it.

So before you start you should ask your self, am i interested on the value or not.

To achieve your goal you have multiple options but most of them refer to Format and Formatter, but i just suggest to look in this answer.

Community
  • 1
  • 1
  • Reverse the operation? Quiet the opposite: formatting a number as a string should be done for system output only and reversing it should not be needed in a well designed system. – Alexis Dufrenoy Jul 24 '15 at 08:21