I need the rounding logic in the following pattern..for 2.23 it should be 2.2 ,for 2.26 it should be 2.3... Please Help out
Asked
Active
Viewed 589 times
-5
-
and whats about 2.25 ? – rpc1 Apr 23 '15 at 06:23
-
for 2.25 it should be 2.3 – Phulkala Narvekar Apr 23 '15 at 06:24
-
2Welcome to SO:SE. The principle on the site is to help user after they have shown their own effort to solve their problems. Such effort may be to search online with keywords like `java rounding`. See [ask] to improve your post please. – mins Apr 23 '15 at 06:24
-
`Math.ceil` and `Math.floor `methods!! you can try – Prashant Apr 23 '15 at 06:25
-
1This might help you http://stackoverflow.com/questions/153724/how-to-round-a-number-to-n-decimal-places-in-java – 0o'-Varun-'o0 Apr 23 '15 at 06:25
2 Answers
1
double a = <ur NUmber>;
double roundOff = (double) Math.round(a*10)/10;
Hope this would help you. here 2.25 would be round off to 2.3

raki
- 195
- 1
- 10
0
If you want to print, than use printf
method, It has default rounding.
System.out.printf("%.1f", 2.23);
System.out.printf("%.1f", 2.26);
If you need rounding value for calculation than use Math.round
double newValue= (double)Math.round(value*10)/10;

Masudul
- 21,823
- 5
- 43
- 58