How to round a floating point number and remove digits after decimal point in C++

1 Answer

0 votes
#include <iostream>
#include <cmath>

int main()
{
    double x = 4.311, y = 6.500, z = 9.811;
    
    std::cout << std::trunc(x) << std::endl;
    std::cout << std::trunc(y) << std::endl;
    std::cout << std::trunc(z) << std::endl;

    x = -4.311, y = -6.500, z = -9.811;
    
    std::cout << std::trunc(x) << std::endl;
    std::cout << std::trunc(y) << std::endl;
    std::cout << std::trunc(z) << std::endl;
}


    
/*
run:
    
4
6
9
-4
-6
-9
    
*/

 



answered Sep 18, 2024 by avibootz
...