Round a number to a specific decimal place, round numbers to int, 10s, 100 and 1000 - C++ Data Type

C++ examples for Data Type:double

Description

Round a number to a specific decimal place, round numbers to int, 10s, 100 and 1000

Demo Code

#include <math.h>
#include <iostream>

int roundToInteger(double);
int roundToTenths(double);
int roundToHundreths(double);
int roundToThousandths(double);

int main(int argc, const char *argv[]) {
    double num = 0.0f;
    while (num != -1) {
        std::cout << "Enter number to round (-1 to exit): ";
        std::cin >> num;//from  w w w  .j  av  a  2  s .c  o m

        std::cout << "Original: " << num
                  << "\nroundToInteger: " << roundToInteger(num)
                  << "\nroundToTenths: " << roundToTenths(num)
                  << "\nroundToHundredths: " << roundToHundreths(num)
                  << "\nroundToThousandths: " << roundToThousandths(num)
                  << std::endl;
    }
    return 0;
}
int roundToInteger(double num) { return floor(num + .5); }
int roundToTenths(double num) { return floor(num * 10 + .5) / 10; }
int roundToHundreths(double num) { return floor(num * 100 + .5) / 100; }
int roundToThousandths(double num) { return floor(num * 1000 + .5) / 1000; }

Result


Related Tutorials