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
floating point - How do I write only the decimal places in C language? - Stack Overflow
If I have 2.55, how do I write only .55 and skip 2 in programming language? 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
๐ŸŒ
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
๐ŸŒ
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; }
๐ŸŒ
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
๐ŸŒ
YouTube
youtube.com โ€บ watch
Setting Decimal Precision in C - YouTube
Setting Decimal Precision in CGreetings, in this C tutorial we shall be looking at how to specify how many decimal places we print out for a float or double ...
Published ย  July 15, 2023
๐ŸŒ
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 output fl...
๐ŸŒ
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.
๐ŸŒ
Academic Help
academichelp.net โ€บ coding โ€บ c-coding โ€บ how-to-print-a-double.html
How to Print a Double in C With Precision
January 31, 2024 - Double-precision floating-point numbers have limited precision, which can lead to rounding errors. To mitigate rounding errors, limit the number of decimal places you print using the %.<n>f format specifier, as shown earlier.