Navigation

Own programs

Snippets

Libraries

Deutsch

roundTo(), floorTo(), ceilTo()

This functions, actually roundTo(long number, long roundingSteps) etc, "round" integers. The example below should show how it works.

You can download the test class here: roundto.zip

Example:

001: #include <iostream>
002: using namespace std;
003: 
004: long floorTo (long number, long roundingSteps) {
        //[...]
007: }
008: 
009: long ceilTo (long number, long roundingSteps) {
        //[...]
011: }
012: 
013: long roundTo (long number, long roundingSteps) {
       //[...]
020: } 
021: 
022: int main(int argc, char* argv[]) {
023:         int number1 = 43234;
024: 	cout << "number is " << number1 << endl;
025: 	cout << "number (floored to 5000) is\t" << floorTo((long)number1, 5000l) << endl;
026: 	cout << "number (ceiled to 5000) is\t" << ceilTo((long)number1, 5000l) << endl;
027: 	cout << "number (rounded to 5000) is\t" << roundTo((long)number1, 5000l) << endl << endl;
028: 
029: 	long number2 = 82168728l;
030: 	cout << "number is " << number2 << endl;
031: 	cout << "number (floored to 3000000) is\t" << floorTo((long)number2, 3000000l) << endl;
032: 	cout << "number (ceiled to 3000000) is\t" << ceilTo((long)number2, 3000000l) << endl;
033: 	cout << "number (rounded to 3000000) is\t" << roundTo((long)number2, 3000000l) << endl << endl;
034: 	
035: 	cout << "press enter to quit" << endl;
036: 	getchar();
037: }
038: 

Output:

number is 43234
number (floored to 5000) is     40000
number (ceiled to 5000) is      45000
number (rounded to 5000) is     45000

number is 82168728
number (floored to 3000000) is  81000000
number (ceiled to 3000000) is   84000000
number (rounded to 3000000) is  81000000

press enter to quit