Rounding
Could anyone tell me a function that would let me round a number to the nearest 25. I''m using Dev c++ if that makes a differnce.
Thanks in advance.
Group Who
cast it as int! divide by 25 and multiply by 25! should go?
"be fair! ... always"Zeusel
No that isn't what I mean. For example if the user enters a 28 the will automatically be rounded down to 25, if the user enters 46 it is rounded up to 50. I need a function to do this automatically so that the program can process it depending on what value the user inputs.
Thanks.
[edited by - ank2 on April 23, 2002 4:18:25 PM]
Thanks.
[edited by - ank2 on April 23, 2002 4:18:25 PM]
Group Who
function RoundTo25(Value:Double):Integer;begin Result:=Round(Value/25)*25;end;
Thanks but i'm using C. I tried translating it but I can't. Is there a similar way to do this in C? Is there a similar function to the Round function you showed for C?
[edited by - ank2 on April 23, 2002 4:48:03 PM]
[edited by - ank2 on April 23, 2002 4:48:03 PM]
Group Who
Well, I don''t know that C++ has a round() function, but to translate that code, it''s like this:
float RoundTo25(double d)
{
return (Round(d/25))*25;
}
If thispost = 0 Then
GoBack()
Else
Read()
End If
float RoundTo25(double d)
{
return (Round(d/25))*25;
}
If thispost = 0 Then
GoBack()
Else
Read()
End If
I'll have a link to the TriFaze website as soon as possible. It's still currently being designed, by myself of course! =) I'll update the URL and my signature as soon as possible.Feel free to send me a message on Yahoo! or AOL IM™. =)
April 23, 2002 04:50 PM
Try something like this:
int RoundTo25(int num){int round = num%25;if(round > 12)num += (25-round);else num -= round;return num;}
April 23, 2002 05:03 PM
Doesn''t anyone test the code they post?
roundTo( 25, 0 ) == 0
roundTo( 25, 25 ) == 25
roundTo( 25, 26 ) == 25
roundTo( 25, 44 ) == 50
roundTo( 25, 1234 ) == 1225
etc.
int roundTo( int iTarget, int iSource ){ return( ( ( iSource + ( iTarget / 2 ) ) / iTarget ) * iTarget );}
roundTo( 25, 0 ) == 0
roundTo( 25, 25 ) == 25
roundTo( 25, 26 ) == 25
roundTo( 25, 44 ) == 50
roundTo( 25, 1234 ) == 1225
etc.
ya. the other ones were wrong.
it''s just, as the guy said above:
rounded = int((num + .5 )) / 25 * 25
it''s just, as the guy said above:
rounded = int((num + .5 )) / 25 * 25
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement