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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Rounding to the nearest 5?? 1

Status
Not open for further replies.

Palmbak

Technical User
Dec 17, 2002
25
US
Does anyone know how to round a number to the nearest 5.00?

Ex. If I have 123.54, I want to round it to 125.00 or if I have 127.52 I would want that rounded to 130.00.

Thanks,

I am using Crystal Reports 8.5


Jon
 
Dim X as Number
Dim R as Number

X = 123.59

R = Remainder(x,5.)

If R < 2.5 Then
Formula = X - R
ElseIf R >= 2.5 Then
Formula = X + (5-R)
End If
 
I don't think that does it JC

My approach would be to truncate the number to the first place left of the decimal and do a subtraction

WhilePrintingRecords
numbervar x := {table.numbertoround};
numbervar temp;
numbervar test;

temp := truncate(x,-1);
test := x - temp;

If test > 5 then
temp := Temp + 10
else if test > 0 then
Temp := Temp + 5
else
Temp; // the case where the number like 120

Temp; Jim Broadbent
 
Dear Jim Broadbent

Can you explain whhy you think JC's formula will not work?

Regards
Krishna Kumar
 
If I had to do such a thing, I would divide by five, then round to the nearest whole number, and finally multiply by five again.

To do this in Crystal is trickier than in a language that lets you define your own data fields. I'd suppose that you'd need a chain of formula fields to manage it. But with a bit of trial and error it should be possible.

As to whether the other methods would work, I can't comment. Madawc Williams
East Anglia
Great Britain
 
Dear Madawc Williams

OK. That is another good method.

But theoritically JC's formula have to work since he is finding out the remainder and adding/subtracting the remainder.

Regards
Krishna Kumar

 
Krishna,

In the example that Palmbak gave, he illustrated scenarios whereby the nearest five always resulted in rounding the original number UP.

If you understand &quot;round a number to the nearest 5.00&quot; (combined with him examples) to mean rounding UP, then use Ngolem's example.

Otherwise, if rounding to the nearest 5 means (as I suspect it might) rounding to the nearest number divisible by 5, regardless if the number may end up being rounded DOWN, then JoeCrystal's formula is sound.

Naith
 
Naith

Ok. Things are clear now. I didnt give close attention to the example.

Palmbak , Now please clarify what should happen if the no is 121.23 :)

Thanks Naith.

Regards
Krishna Kumar

 
numberVar X:= {@%OfNatRates};
numberVar R:= Remainder(X,5.);


If R < 2.5 Then
X - R
Else if R >= 2.5 Then
X + (5-R)

This was the formula that used and it works....

Thanks JoeCrystal.

Jon Palmbak
 
Krishna

In the example given...there was no discussion about rounding to the nearest &quot;5&quot; it was either to 120 or 130....not to 120 ,125 or 130 so JC's formula would not work.

I should have perhaps explained my reason better.

I think both of our methods fail with negative numbers though on a quick glance. Jim Broadbent
 
Hi Jim

Thanks for the explanation. Things are clear now.

Regards
Krishna Kumar
 
I suppose on re-reading I read the question wrong...sorry Jim Broadbent
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top