Write a one line C function to round floating point numbers Last Updated : 02 Jun, 2017 Comments Improve Suggest changes Like Article Like Report 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 */ # include<stdio.h> int roundNo(float num) { return num < 0 ? num - 0.5 : num + 0.5; } int main() { printf("%d", roundNo(-1.777)); getchar(); return 0; } Output: -2 Time complexity: O(1) Space complexity: O(1) Now try rounding for a given precision. i.e., if given precision is 2 then function should return 1.63 for 1.63322 and -1.63 for 1.6332. Comment More infoAdvertise with us Next Article Write a one line C function to round floating point numbers K kartik Follow Improve Article Tags : C Language c-puzzle C-Data Types Similar Reads Rounding Floating Point Number To two Decimal Places in C and C++ 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 cou 2 min read 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 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 Formatted and Unformatted Input/Output functions in C with Examples In C language, the Input/Output (I/O) functions are part of the standard library, and these functions are used for interacting with the user or other systems, to perform operations such as reading input and printing output. These functions provide ways to read data from files and other input devices 7 min read <cfloat> float.h in C/C++ with Examples This header file consists of platform-dependent and implementation specific floating point values. A floating point has four parts. Sign Its value can be either negative or non-negative. Base It is also known as radix of exponent representation which represents different numbers with single number i 4 min read Like