Rounding Floating Point Number To two Decimal Places in C and C++ Last Updated : 21 May, 2024 Comments Improve Suggest changes Like Article Like Report How to round off a floating point value to two places. For example, 5.567 should become 5.57 and 5.534 should become 5.53 First Method:- Using Float precision C++ #include<bits/stdc++.h> using namespace std; int main() { float var = 37.66666; // Directly print the number with .2f precision cout << fixed << setprecision(2) << var; return 0; } // This code is contributed by shivanisinghss2110 C #include <stdio.h> int main() { float var = 37.66666; // Directly print the number with .2f precision printf("%.2f", var); return 0; } Output37.67Time Complexity: O(1), as the execution time does not depend on the size of the input. Space Complexity: O(1), as it only uses a fixed amount of memory to store a single float variable and does not allocate any dynamic memory. Second Method: Using integer typecast If we are in Function then how return two decimal point value 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; } Output:37.67Time complexity:The round function only performs a few basic arithmetic operations, so its time complexity is constant or O(1). Space complexity:The round function creates a single float variable named value, so its space complexity is constant or O(1). Third Method: using sprintf() and sscanf() C++ #include <iostream> using namespace std; float round(float var) { // we use array of chars to store number // as a string. char str[40]; // Print in string the value of var // with two decimal point sprintf(str, "%.2f", var); // scan string value in var sscanf(str, "%f", &var); return var; } int main() { float var = 37.66666; cout << round(var); return 0; } Output:37.67 Comment More infoAdvertise with us Next Article Rounding Floating Point Number To two Decimal Places in C and C++ D Devanshu Agarwal Improve Article Tags : Misc C Language cpp-data-types Practice Tags : Misc Similar Reads Convert a floating point number to string in C Write a C function ftoa() that converts a given floating-point number or a double to a string. Use of standard library functions for direct conversion is not allowed. The following is prototype of ftoa(). The article provides insight of conversion of C double to string. ftoa(n, res, afterpoint) n -- 3 min read Write a one line C function to round floating point numbers Algorithm: roundNo(num) 1. If num is positive then add 0.5. 2. Else subtract 0.5. 3. Type cast the result to int and return. Example: num = 1.67, (int) num + 0.5 = (int)2.17 = 2 num = -1.67, (int) num - 0.5 = -(int)2.17 = -2 Implementation: c /* Program for rounding floating point numbers */ # inclu 1 min read Precision of Floating Point Numbers in C++ (floor(), ceil(), trunc(), round() and setprecision()) The decimal equivalent of 1/3 is 0.33333333333333â¦. An infinite length number would require infinite memory to store, and we typically have 4 or 8 bytes. Therefore, Floating point numbers store only a certain number of significant digits, and the rest are lost. The precision of a floating-point numb 4 min read Program to compute division upto n decimal places Given 3 numbers x, y and n compute the division (x/y) upto n decimal places. Examples : Input : x = 22, y = 7, n = 10Output : 3.1428571428Explanation : Since n = 10, division (x / y) is taken till 10 decimal places. Input : x = 22, y = 7, n = 20Output : 3.14285714285714285714 Approach : Get the rema 7 min read Check whether given floating point number is even or odd Given a floating-point number, check whether it is even or odd. We can check whether a integer is even or odd by dividing its last digit by 2. But in case of floating point number we can't check a given number is even or odd by just dividing its last digit by 2. For example, 100.70 is an odd number 6 min read Like