0

I have a double value a = 0.00059 and an Integer value which gets incremented and multiplied with the double value (say b = 1)

when I set the answer to the textview

//for b = 1
view.setText(((double)(a*b)));

the answer I get is " 5.9E-4 " however it should be 0.00059.

am I multiplying the values correctly.?

user2933671
  • 273
  • 3
  • 5
  • 19
  • Try with view.setText(String.valueOf((double)(a*b))); – Mauro Valvano Apr 18 '14 at 16:41
  • The value it gives is correct, but it's the scientific notation. – AntonH Apr 18 '14 at 16:42
  • 2
    possible duplicate of [How to print double value without scientific notation using Java?](http://stackoverflow.com/questions/16098046/how-to-print-double-value-without-scientific-notation-using-java) is what I suspect you're looking for. – Brian Roach Apr 18 '14 at 16:46

3 Answers3

4

In addition to the other answers provided, you can use a formatter:

NumberFormat formatter = new DecimalFormat("#.#####");
view.setText(formatter.format(a*b));
Amir Afghani
  • 37,814
  • 16
  • 84
  • 124
3

You are multiplying them correctly. The values 5.9E-4 and 0.00059 are equivalent, mathematically and programmatically. The representation 5.9E-4 is like scientific notation, i.e. 5.9x10^(-4) is equivalent to 0.00059.

rgettman
  • 176,041
  • 30
  • 275
  • 357
3

You get the same value as you want to get, but formatted in a scientific notation. What you need to do is to explicitly convert it to String:

view.setText(String.format("%f", a*b));

And you could eventually specify the number of decimal places to print after the decimal separator in this way:

// displays two digits after the decimal separator
view.setText(String.format("%.2f", a*b));  
Denis Kulagin
  • 8,472
  • 17
  • 60
  • 129