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
๐ŸŒ
Cprogramming
cboard.cprogramming.com โ€บ c-programming โ€บ 119573-round-float-2-decimal-places.html
Round float to 2 decimal places
with printf() you can specify the amount of decimal places > %.0f = no decimal places, %.4f equals float showing four decimal places; whether or not this is the exact value stored is another matter...but at least you will be displaying the rounded up one you want. ... There might be a more elegant way of doing this in C, but you are posting C++ code here in the C forum so I have an idea that might work on both: ... float num = 3.14159; num *= 100; if(num >= 0) num += 0.5; else num -= 0.5; long round = num; num = round; num /= 100; ... float and double are not designed to handle monetary amounts.
๐ŸŒ
Cplusplus
cplusplus.com โ€บ forum โ€บ beginner โ€บ 222475
Rounding offto 2 decimal points - C++ Forum
For rounding methods, check out http://www.cplusplus.com/forum/articles/3638/ (and its follow-up http://www.cplusplus.com/articles/213hAqkS/). Whew. Hope this helps! ... Round to nearest integer, rounding halfway cases away from zero: std::round std::lround std::llround http://en.cppreference.com/w/cpp/numeric/math/round Also see: std::nearbyint http://en.cppreference.com/w/cpp/numeric/math/nearbyint std::rint std::lrint std::llrint http://en.cppreference.com/w/cpp/numeric/math/rint
๐ŸŒ
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; } Output: 37.67 ยท
๐ŸŒ
YouTube
youtube.com โ€บ portfolio courses
Round A Double To A Specific Number Of Decimal Places | C Programming Example - YouTube
How to round a double value to a specific number of decimal places in C, for example rounding a double value to 2 decimal places, or rounding a double to 1 d...
Published ย  September 11, 2022
Views ย  13K
๐ŸŒ
Oxford University
mathcenter.oxford.emory.edu โ€บ site โ€บ cs170 โ€บ printf
The printf Method
we now see that the format specifier "%.2f" tells the printf method to print a floating point value (the double, x, in this case) with 2 decimal places. Similarly, had we used "%.3f", x would have been printed rounded to 3 decimal places.
Find elsewhere
๐ŸŒ
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 ...
๐ŸŒ
Tuicocach
tuicocach.com โ€บ round-and-display-2-decimal-places-in-c-and-c
Round and display 2 decimal places in C and C++
#include <stdio.h> #include <math.h> ... yourself. To round to a specific decimal place, you can multiply the number by 10^n, round it, and then divide it by 10^n....
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ cplusplus-program-to-round-a-number-to-n-decimal-places
C++ Program to Round a Number to n Decimal Places
In this article, we shall cover a few techniques to represent a floating-point number up to a certain decimal placed by rounding it off. Among different methods, using C like formatting strings, using precision parameters, and using the round() function present in the math library is important. ...
๐ŸŒ
Medium
medium.com โ€บ @mihirdesai33 โ€บ how-to-round-a-double-to-2-decimal-places-in-c-8c50770d1b0e
How to round a double to 2 decimal places in C | by Mihir Desai | Medium
July 13, 2023 - You can use this function along with some math to round a double to two decimal places. The idea is to multiply the double by 100, round it to the nearest integer, and then divide it by 100 to obtain the rounded result.
๐ŸŒ
Quora
quora.com โ€บ How-do-you-round-to-2-decimal-places-in-C
How to round to 2 decimal places in C++ - Quora
Answer (1 of 5): > How do you round to 2 decimal places in C++? Rounding a floating-point value to 2 decimal places is not a useful concept (unless you happen to be using a rare implementation that uses decimal, rather than binary, floating point). Hereโ€™s a program that includes a not particula...
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ c round() function
C round() Function - Scaler Topics
March 27, 2024 - The round() function in C returns the nearest integer value (rounded off value) of the number provided as an argument which can be of type float, integer, or double number provided as an argument.
๐ŸŒ
Reddit
reddit.com โ€บ r/c_programming โ€บ rounding in c
r/C_Programming on Reddit: Rounding in C
May 8, 2024 -

I have a question when it comes to rounding in C. Does it round up or down at .5? If it does round up, then does that mean that the smallest value of k in the code below can only be 1?

 int main()
{
    int k = 13;
    int i;
    for (i = 0; i < 8; i++) {
        printf("%d", (k%2));
        k >>= 1;
    }
    printf("%n");
}

๐ŸŒ
OpenGenus
iq.opengenus.org โ€บ rounding-and-truncating-numbers-in-c
Rounding and Truncating numbers using math.h in C
November 10, 2019 - #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 } Output: roundf(+2.311) gives = +2.0 ยท If we pass double as an argument, the syntax is: round (double arg); Example: #include ...