If you just want to round the number for output purposes, then the "%.2f" format string is indeed the correct answer. However, if you actually want to round the floating point value for further computation, something like the following works:

#include <math.h>

float val = 37.777779;

float rounded_down = floorf(val * 100) / 100;   /* Result: 37.77 */
float nearest = roundf(val * 100) / 100;  /* Result: 37.78 */
float rounded_up = ceilf(val * 100) / 100;      /* Result: 37.78 */

Notice that there are three different rounding rules you might want to choose: round down (ie, truncate after two decimal places), rounded to nearest, and round up. Usually, you want round to nearest.

As several others have pointed out, due to the quirks of floating point representation, these rounded values may not be exactly the "obvious" decimal values, but they will be very very close.

For much (much!) more information on rounding, and especially on tie-breaking rules for rounding to nearest, see the Wikipedia article on Rounding.

Answer from Dale Hagglund on Stack Overflow
🌐
Reddit
reddit.com › r/c_programming › maximum decimal places
r/C_Programming on Reddit: Maximum decimal places
June 10, 2020 -

What is the maximum decimal places that can be stored using c? I tried to store - 1.86264514923107200935514487085e-09 but only got 0.00000000186264514923107200000000.....

Top answer
1 of 5
3
https://en.wikipedia.org/wiki/Double-precision_floating-point_format Significant precision is 53 bits = 9.00719925e15, so that's 15 (almost 16) decimal digits of significance
2 of 5
3
If you care about preserving the value across a complete decimal → floating-point → decimal round-trip: #include #include int main(void) { printf("FLT_DIG = %d\n", FLT_DIG); printf("DBL_DIG = %d\n", DBL_DIG); printf("LDBL_DIG = %d\n", LDBL_DIG); } On my system, I get: FLT_DIG = 6 DBL_DIG = 15 LDBL_DIG = 18 Note that this does not mean that all values with at most this many decimal digits can be represented "exactly". It just means that the round-trip will yield the same decimal digits at the end. Note also that "decimal digits" here is not just the digits after the decimal point. 1234.56 has 6 significant decimal digits. If stored in a float and then reformatted as a decimal value, it will yield the same decimal value on my system. But 1234.567 has 7 significant decimal digits, and this is not guaranteed to yield the same decimal value if round-tripped through a float on my system. If you care about a floating-point → decimal → floating-point round-trip, you need more decimal digits: #include #include int main(void) { printf("FLT_DECIMAL_DIG = %d\n", FLT_DECIMAL_DIG); printf("DBL_DECIMAL_DIG = %d\n", DBL_DECIMAL_DIG); printf("LDBL_DECIMAL_DIG = %d\n", LDBL_DECIMAL_DIG); } I get: FLT_DECIMAL_DIG = 9 DBL_DECIMAL_DIG = 17 LDBL_DECIMAL_DIG = 21 These values are greater than the ones before because round-tripping in this direction needs to preserve the precision in the original floating-point value.
Discussions

how do u remove value after decimal in c like 69.000000 i want to remove the 0 after decimal
Post a snipper of your current code and we'll tell you how to best modify it to suit your behaviour. It depends on how you want other, non-round values to behave, i.e. if you want 69.2 to be 69 or 69.2 Vaguely, your options are: Cast it to int, and print as an int Round up / down Use the correct format specifier if you're using printf (so %2.0f or %g) More on reddit.com
🌐 r/cprogramming
7
4
April 14, 2023
How to limit user to input a float with one decimal point in C - Stack Overflow
Program to limit the user's input to one decimal point in C. For example: Enter grade: 5 //Acceptable Enter grade: 8.5 //Acceptable Enter grade: 6.467 //Not acceptable, Re-enter grade #include... More on stackoverflow.com
🌐 stackoverflow.com
Maximum decimal places
https://en.wikipedia.org/wiki/Double-precision_floating-point_format Significant precision is 53 bits = 9.00719925e15, so that's 15 (almost 16) decimal digits of significance More on reddit.com
🌐 r/C_Programming
6
1
June 10, 2020
[C] Truncating to 2 decimal places.
Using the stdlib floor function from math.h should help you (I didn't try this and my C is a bit rusty, so ... :-) ): #include double floor( double arg ); double cutoff = floor(input * 100) / 100; More on reddit.com
🌐 r/learnprogramming
2
1
September 26, 2014
🌐
GeeksforGeeks
geeksforgeeks.org › c language › g-fact-41-setting-decimal-precision-in-c
Setting decimal precision in C - GeeksforGeeks
January 10, 2025 - How to print floating point numbers with a specified precision? Rounding is not required. For example, 5.48958123 should be printed as 5.4895 if given precision is 4. For example, below program sets the precision for 4 digits after the decimal point: ... // C program to set precision in floating point numbers #include<stdio.h> #include<math.h> int main() { float num = 5.48958123; // 4 digits after the decimal point num = floor(10000*num)/10000; printf("%f", num); return 0; }
🌐
W3Schools
w3schools.com › c › c_data_types_dec.php
C Data Types Decimal Precision
You have probably already noticed ... Yourself » · If you want to remove the extra zeros (set decimal precision), you can use a dot (.) followed by a number that specifies how many digits that should be shown after the decimal ...
🌐
YouTube
youtube.com › coffee programmer
C Programming Limiting floats to two decimal places - How to limit and display float variable values - YouTube
Can you help me to buy a coffee:https://www.buymeacoffee.com/coffeeprogram how to limit float to 2 decimal places in c
Published   October 12, 2022
Views   2K
🌐
Northern Michigan University
nmu.edu › Webb › ArchivedHTML › MathComputerScience › c_programming › c_008.htm
More About Float and Double Variables
To change the number of decimal places printed out for float or double variables, modify the %f or %e to include a precision value, eg, ... In this case, the use of %.2f limits the output to two decimal places, and the output now looks like
🌐
Quora
quora.com › How-do-you-handle-the-rounding-of-decimal-places-in-C-C-float-rounding-development
How to handle the rounding of decimal places in C (C, float, rounding, development) - Quora
The C standard math library has ... 3 decimal places - then multiply the number by 1000, use the ceil, floor or round function depending on which way you want to round - then divide by 1000....
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › c language › rounding-floating-point-number-two-decimal-places-c-c
Rounding Floating Point Number To two Decimal Places in C and C++ - GeeksforGeeks
May 21, 2024 - #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; }
🌐
Quora
quora.com › How-do-I-round-off-a-float-to-2-decimal-points-in-C-like-3-01943-to-3-02
How to round off a float to 2 decimal points in C, like 3.01943 to 3.02 - Quora
Answer (1 of 7): If you have to round input floating point number then for that the format is “%wf” where w = Integer number for total width of data.(including the digit before and after decimal and decimal itself) Ex- scanf(“”,&x) For x = 3.01943 Result = 7.8 If you have to round ...
🌐
Linux Hint
linuxhint.com › setting_decimal_precision_c_-language
Setting Decimal Precision in C – Linux Hint
One method of adjusting the decimal precision of a variable is to multiply it by a factor of 10, 100, or some other multiple of 10 depending on what precision you want to achieve, and round the result with the ceilf() function.
🌐
Quora
quora.com › How-do-you-get-6-decimal-places-in-C-How-do-you-limit-decimal-places-in-C
How to get 6 decimal places in C++? How do you limit decimal places in C++ - Quora
Answer (1 of 8): If you are just printing the number by rounding it off to 6 decimal place then use [code]printf("%.6f",N); [/code]or you can also use [code]cout using namespace std; int main() { double N ...
🌐
Homedutech
homedutech.com › program-example › how-to-restrict-a-float-value-to-only-two-places-after-the-decimal-point-in-c.html
How to restrict a float value to only two places after the decimal point in C?
In C, you can restrict a float value to only two decimal places for display purposes by using the printf function with a format specifier that specifies the precision.
🌐
w3tutorials
w3tutorials.net › blog › how-do-i-restrict-a-float-value-to-only-two-places-after-the-decimal-point-in-c
How to Restrict a Float to Two Decimal Places in C: Rounding Techniques Explained — w3tutorials.net
To solve this, we need to explicitly restrict the number of decimal places—either for display (e.g., showing 0.33 instead of 0.333333343) or for further computation (e.g., storing 0.33 as the actual value).
🌐
Oxford University
mathcenter.oxford.emory.edu › site › cs170 › printf
The printf Method
How do we print just an approximation of a variable's value (to some number of decimal places)? Interestingly, it is possible to creatively use casting and promotion to print a "double" value with only two decimal places.
🌐
OpenGenus
iq.opengenus.org › rounding-and-truncating-numbers-in-c
Rounding and Truncating numbers using math.h in C
November 10, 2019 - It takes a floating value as argument and rounds the decimal part of it. It returns value in floating type format. If we pass float number as an argument, the syntax is: ... #include <stdio.h> #include <math.h> int main() { printf("roundf(+2.311) gives = %+.1f\n", roundf(2.311)); //here %+.1f is used to print only one place after decimal }
🌐
Cprogramming
cboard.cprogramming.com › c-programming › 161290-how-truncate-round-decimal.html
How to Truncate and Round a Decimal?
January 14, 2014 - #include <stdio.h> int main() { double num, trunc, round; printf("\nEnter a Decimal:\n\n"); scanf("%lf", &num); trunc = num/10*10; printf("Truncated Value%.1lf\n\n\n", trunc); round = (num)/10*10; printf("Rounded Value%.1lf\n\n\n", round); system("pause"); return 0; } That's my code currently, I've gotten it to round as I want, but I want it to truncate to one decimal place as well.
🌐
Digital Pooch
digitalpooch.com › home › how to limit decimal places in c++: all the stats, facts, and data you’ll ever need to know
how to limit decimal places in c++: All the Stats, Facts, and Data You'll Ever Need to Know - Digital Pooch News
October 28, 2021 - How many decimal places does a pointer get for a given number? It doesn’t matter, but your expression could be more complicated. You may be thinking “But I just want to print a decimal places in an expression.” That’s why I recommend using at least one decimal place.