Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CPP

round up 2 digits float c++

#include <iostream>
using namespace std;
float round(float var)
{
    // 37.66666 * 100 =3766.66
    // 3766.66 + .5 =3767.16    for rounding off value
    // then type cast to int so value is 3767
    // then divided by 100 so the value converted into 37.67
    float value = (int)(var * 100 + .5);
    return (float)value / 100;
}
 
int main()
{
    float var = 37.66666;
    cout << round(var);
    return 0;
}
Source by www.geeksforgeeks.org #
 
PREVIOUS NEXT
Tagged: #digits #float
ADD COMMENT
Topic
Name
9+2 =