Solved

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

Posted on 2004-08-02
7
1,180 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

615 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