Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

rounding number to a half 1

Status
Not open for further replies.

Triion

Programmer
Sep 20, 2005
6
BE
Hi,

I have a application in which i need to 'round' number to there closest 'clean' number... meaning:

number should become 'nice' number
4.3235... -> 4.5
0.0068... -> 0.01
0.0035... -> 0.005
35.2154 -> 35.5

So it's always rounding to the above number, but I don't know how to make a methode that would do this... decimal number should stay decimal, with Math.ceil 0.005 becomes 1 ! and I don't know hoz to round to the closest HALF...
I hope you understand what I mean...

thanx in advance,
triion
 
Should 4.3325 not be rounded to 4.0 ?

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
I think the problem is the concept of "clean number".

You should define some rules about what requisites a "clean number" must follow.

Cheers,
Dian
 
well, What I would like to have is a function that would round to the nearest number with a 1 or a 5 in it...

well 0.003 should infact become 0.005

If the number is a large number like 140 then the .003 doesn't really matter...

I need this for making a tickMark scale on an axis...

for instance: the values to be mapped are from 0 to 1000
and the axis is 300 pixels... thus 3.3333333 per pixel...
I want for instance every 10 pixels a tick, so a per tick that would be 33.3333 value/tick. Not a nice number for displaying at each tick !! taking 34 per tick would be to much, part of the axis would not be used... 33 would mean that the axis is to small ! I thought 33.5 would be better... Ofcourse the values change for each graphic so I need some kind of method for rounding those numbers...

Hope its clearer now...

thanx in advance,
triion
 
Maybe a little clunky and untested, but how about:

Code:
float myNum = 123.321; //for example

float x = myNum; //start the process
x *= 10; //x = 1233.21
int xInt = Math.round(x); //xInt = 1233
int higher = xInt;
int lower = xInt;
while(higher % 5 != 0 && lower % 5 != 0)
{
 higher += 1;
 lower -= 1;
}//end while

float marker;
if(higher % 5 == 0)
 marker = higher; //1235
else
 x = lower;

marker /= 10; //123.5


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
...east is east and west is west and if you take cranberries and stew them like applesauce
they taste much more like prunes than rhubarb does
[infinity]
 
Okay, now I tested it. Make sure that if you run a similar test, that you initialize the first value with the 'F' for float:

float myNum = 123.321[!]F[/!];

Dave

P.S., additional tests showed:
89.02 converted to 89.0
89.25 converted to 89.5
89.245 converted to 89.0

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
...east is east and west is west and if you take cranberries and stew them like applesauce
they taste much more like prunes than rhubarb does
[infinity]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top