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

Division operator that returns an integer

Status
Not open for further replies.

nhungr

Programmer
Jan 31, 2005
26
CA
Hello.

Is there an operator or function in C++ that allows you to divide two numbers and return just the integer part (ie. the number on the left side of the decimal point)? The opposite of the modulus (%) operator!

Examples:
7 / 2 would return the integer 3
14 / 3 would return the integer 4

There should be no rounding up to the nearest integer!

Thanks,
Nikolai.
 
If you store or cast the return value to an int, then only the integer part will be there. If your operands are integers, then this is done for you.

There is also a function in <cmath> called floor that will work for doubles and can be used on the result of double division.

If you might be working with negative numbers pay special attention to how these two solutions handle them.
 
Hi,

I think the function floor() will work for you. It will round down to the nearest integer.

Regards,

David
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top