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

How can i make rounding?

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Good afternoon,
i have a problem. i don't know the function to make a rounding. for example, i have a const = 4,1234
there is a function to obtain 4,12 ?
can i decide how many decimals i want?
Best regards and thank you very much!
Michele

p.s. my problem isn't a video format but if i must make any mathematical operation.
 
float a;
int a1;

a=4.12345; // may be any positive value
a=(4.1234*100+0.5); //value of a = 412.84
a1=a; // a1=412 because of type conversion
a=a1/100; // a=4.12;
hnd
hasso55@yahoo.com

 

int round(int value, int decimalPlace)
{
int roundOff = 10*decimalPlace;

if(roundOff > value)
return 0;

int roundUpOrDown = ((value % roundOff)/(roundOff/10)) >= 5 ? 1:0;

return value%roundOff + roundoff*roundUpOrDown;
}


that should do your rounding for integer values... for decimals, see what hnd did above and combine it with this

Matt
 

int round(int value, int decimalPlace)
{
int roundOff = 10*decimalPlace;

if(roundOff > value)
return 0;

int roundUpOrDown = ((value % roundOff)/(roundOff/10)) >= 5 ? 1:0;

return value - (value%roundOff) + roundoff*roundUpOrDown;
}

// sorry if this is a double post... i clicked submit before realizing i forgot one important thing. If this is not a double post please disregard this :)

that should do your rounding for integer values... for decimals, see what hnd did above and combine it with this

Matt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top