2

This might be a very simple questions but i cant find a hint anywhere.

I have check the below links but still cannot find what is wrong with my code.
Returning a double from a method
Easiest way to return double from another class

What i want is to return a method that return a 2 decimal double.

Call the below method but it is not running.

toDouble(double1);//the result are not 2 decimals
edittext.setText(String.valueOf(double1));

public double toDouble(double d){
  String str = String.format("%1.2f", d);
  d = Double.valueOf(str);
  return d;
}

It work well when i code like this

  String str = String.format("%1.2f", double1);
  double1 = Double.valueOf(str);    

Anyone can guide me in this?

Edit 1
If anyone reading this. It is a silly mistake.
**Forgot to define the double

double1 = toDouble(double1);//the result are not 2 decimals
edittext.setText(String.valueOf(double1));
Tony Ming
  • 101
  • 1
  • 1
  • 11

2 Answers2

1

you should pass the value to your method, you by mistake set value directly inside settext

the below code is wrong

edittext.setText(String.valueOf(double1));

instead you should write

edittext.setText(String.valueOf(toDouble(double1)));
Amir
  • 179
  • 2
  • 14
1

Simply do it in a single line:

edittext.setText(String.valueOf(toDouble(double1)))
Sultan Mahmud
  • 1,245
  • 8
  • 18