0

I have number like 24 and I need to return like result 24.0. How can I do this think.

I try with this code but is return 24. If is 24.5 is working correct but for 24 is return like integer.

double sliderValue = Math.round(number*10)/10;

Anybody know how can return 24.0

Evgeni Velikov
  • 362
  • 3
  • 10
  • 28
  • 1
    The value isn't changing, it is how you display it. When you turn it to a string you should use String.format, or Decimal.format. Consider this answer http://stackoverflow.com/a/2538803/2067492 – matt Mar 29 '16 at 08:51

4 Answers4

2

You are confusing internal representation with presentation.

The double with a value of 24.0... will be presented (by default, e.g. when using System.out.print) as 24.

Other methods exist for displaying values with a specified number of digits after the decimal point:

double f = 24;
System.out.printf("%.1f\n", f);   // will print 24.0

You should also perhaps consider using BigNumber instead, which specifically encodes for the number of digits after the decimal point, and treats 24.00 as a different number to 24.0.

Alnitak
  • 334,560
  • 70
  • 407
  • 495
2

You can try this

Solution 1 : Returns double

double value = 12.3457652133

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

Solution 2 : it would return string

example just for demonstration

double Value=i/60000;
String s = (new DecimalFormat("##.#").format(Value));

Hope so this helps

samridhgupta
  • 1,695
  • 1
  • 18
  • 34
0

You can try: (I assume you will only print the result in console and see it)

double double1 = Double.valueOf(24);
double double2 = Double.valueOf(24.5);

System.out.println(double1);
System.out.println(double2);

This will give you the output:

24.0
24.5
Kartic
  • 2,935
  • 5
  • 22
  • 43
0

Use DecimalFormat

double value = 12.3d;
DecimalFormat df = new DecimalFormat("#.00");
System.out.println(df.format(value));
imps
  • 1,423
  • 16
  • 22