0

Hi friends I want to get two values after decimal,but in my I am getting 0 after the decimal. this is my code below.

  if (!ed.getText().toString().equals("")) {
                        if (rb1.isChecked()) {
                            int input = Integer.valueOf(ed.getText().toString());
                            double out = input / 24;
                            out = (double)Math.round(out * 100)/100;
                            Intent m = new Intent(Page.this,MActivity.class);
                            m.putDoubleExtra("res", out);
                            startActivity(m);

I referred these sites also: Android : How to get just two digit after the point in decimal value ? dont want to trunc the value

Java : how do I get the part after the decimal point?

and I tried the code below also but it getting 0 after decimal.

DecimalFormat newFormat = new DecimalFormat("#.##");
double twoDecimal =  Double.valueOf(newFormat.format(d))

Please help me new to coding. Thanks in advance.

Community
  • 1
  • 1
sun
  • 1,832
  • 4
  • 19
  • 31

2 Answers2

1

Since input and 24 are integers, when you divide it, it still produces int. Only then it is converted to double.

One way to solve this: double out = input / 24.0;

Jonathan Darryl
  • 946
  • 11
  • 16
0

DecimalFormat newFormat = new DecimalFormat("#.00"); double twoDecimal = Double.valueOf(newFormat.format(d))

In your code i found is

out = (double)Math.round(out * 100.0)/100.0;

Naitik
  • 101
  • 6