Solved

How can I round a double to the nearest 0.5 or 0.25 ?

Posted on 2004-08-02
7
1,149 Views
Last Modified: 2008-02-26
Hi, how can I round a double to the nearest 0.5 or 0.25 ?

e.g.

rounding to nearest 0.25:

4.08 -> 4
4.19 -> 4.25
5.55 -> 5.50
6.78 -> 6.75
6.98 -> 7

rounding to nearest 0.50:

4.08 -> 4
4.19 -> 4
5.55 -> 5.50
6.78 -> 7
6.98 -> 7
0
Comment
Question by:ycomp
  • 4
  • 2
7 Comments
 
LVL 37

Expert Comment

by:zzynx
ID: 11697123
 return Math.ceil( val * 0.25d ) / 0.25f ;
and
  return Math.ceil( val * 0.5d ) / 0.5f ;
0
 
LVL 37

Expert Comment

by:zzynx
ID: 11697166
Stupid me. That's wrong sorry.
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 11697225
double x = (double)(Math.round(d * 4.0)) / 4;
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 500 total points
ID: 11697237
(that's for nearest .25) - change to 2.0 for nearest .5
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11697255
... and of course 'd' is the original value
0
 

Author Comment

by:ycomp
ID: 11697273
thanks CEHJ
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11697328
:-)
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Unhandled exception type Exception 18 52
pagenation logic how it is working in my code 1 69
Need Help! Getting a syntax error and don't understand why 3 38
JDBC performance 8 28
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question