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
🌐
Quora
quora.com › How-do-I-display-floating-number-with-2-decimal-places-in-c
How to display floating number with 2 decimal places in c - Quora
Here's a good tutorial on format strings which as used by more functions than just printf: Printf format strings - Cprogramming.com ... To print a floating-point number with exactly two decimal places in C, use the printf family with the "%.2f" format specifier.
Discussions

CCS :: View topic - Float Values in Printf() upto 2 decimal values
Profile Log in to check your private messages Log in · CCS does not monitor this forum on a regular basis More on ccsinfo.com
🌐 ccsinfo.com
April 4, 2007
How to format a float variable?
You can specify the format in the printf family of calls, e.g. float x = 5.6f; printf("%.3f", x); will print: 5.600 See man printf Demo More on reddit.com
🌐 r/cprogramming
6
8
January 11, 2023
How can I change a float variable into one with strictly 2 decimal places?
if you dont mean printing: that is not possible. but if you want that you can just use an int or long etc and "think" the decimal dot. floating point numbers dont offer precision as you might expect: 1.05 - 0.05 might equal 0.99997 or something, this is due to technical restrictions: if you dont want the whole range of decimal numbers you can just use ints as theyre more reliable anyways. More on reddit.com
🌐 r/cprogramming
5
3
January 12, 2022
Anyone know how to make a float display up to 2 decimal points?
This question is old, but shows up quite high in search engines. The correct answer currently (in Godot 4.3) is: text = "%0.2f" % total This formats to two digits after the decimal, and 0 or more digits before. More on reddit.com
🌐 r/godot
20
11
February 1, 2022
🌐
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 ·
🌐
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.
🌐
CCS, Inc.
ccsinfo.com › forum › viewtopic.php
CCS :: View topic - Float Values in Printf() upto 2 decimal values
April 4, 2007 - Profile Log in to check your private messages Log in · CCS does not monitor this forum on a regular basis
🌐
IncludeHelp
includehelp.com › c › how-to-print-float-value-till-number-of-decimal-points-using-printf.aspx
How to print float value till number of decimal points using printf in C language?
Consider the program, here we will print 2 digits after the decimal point. #include <stdio.h> int main() { float num = 10.23456f; printf("num = %.2f\n",num); return 0; }
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 ...
🌐
Smith College
science.smith.edu › ~jfrankli › 231f13 › 231 › format.html
Formatting in printf, in C
For example, printf("The value is .6lf",myDoubleNum); causes the double precision value in the variable myDoubleNum to be printed out using 10 characters including the decimal point, with 6 number characters following the decimal point. Recall that the lf coming after the 10.6 stands for long float, or double precision.
🌐
GeeksforGeeks
geeksforgeeks.org › c language › g-fact-41-setting-decimal-precision-in-c
Setting decimal precision in C - GeeksforGeeks
January 10, 2025 - // 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; }
🌐
Sololearn
sololearn.com › en › Discuss › 3253699 › how-to-print-floating-value-of-only-last-2-round-points-in-c
How to print floating value of only last 2 round points in c++?
November 27, 2023 - Sololearn is the world's largest community of people learning to code. With over 25 programming courses, choose from thousands of topics to learn how to code, brush up your programming knowledge, upskill your technical ability, or stay informed about the latest trends.
🌐
Sololearn
sololearn.com › en › Discuss › 3108546 › how-to-f-to-output-the-desired-digit-of-decimal-floatsaka-after-point
How to %f to output the desired digit of decimal & floats(aka ...
November 30, 2022 - Sololearn is the world's largest community of people learning to code. With over 25 programming courses, choose from thousands of topics to learn how to code, brush up your programming knowledge, upskill your technical ability, or stay informed about the latest trends.
🌐
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
🌐
Texas Instruments E2E
e2e.ti.com › support › tools › code-composer-studio-group › ccs › f › code-composer-studio-forum › 804426 › ccs-cc2640r2f-printing-2-decimal-places-using-display_printf
Printing 2 decimal places using Display_printf - Code ... - TI E2E
May 23, 2019 - If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question. ... I need to print the float value upto 2 decimal places. I am using Display_printf function to print the result.
🌐
Caltech
sites.astro.caltech.edu › ~mcs › ovro › 40m › help › DoubleFormat.html
A floating point format specifier.
The format specifier strings are a subset of the double precision formats accepted by the C language printf function, plus an extra "s" style, used for printing numbers in sexagesimal. The following shows the components of a specifier, where optional components are shown here enclosed in square brackets. ... As shown, the specification optionally starts with any combination of the following flag characters.
🌐
Woolsey Workshop
woolseyworkshop.com › home › quick tip: how to format floating point output in c++
Quick Tip: How To Format Floating Point Output In C++ - Woolsey Workshop
June 24, 2020 - #include <iomanip> // std::setprecision, std::setw #include <iostream> // std::cout, std::fixed int main() { float floatA = 3.14159; float floatB = 12.3456; // Standard C formatting method printf("floatA = %5.2f\n", floatA); printf("floatB = %5.2f\n", floatB); std::cout << "==============" << '\n'; // separator // Preferred C++ formatting method std::cout << std::fixed // fix the number of decimal digits << std::setprecision(2); // to 2 std::cout << "floatA = " << std::setw(5) << floatA << '\n'; std::cout << "floatB = " << std::setw(5) << floatB << std::endl; }
🌐
Reddit
reddit.com › r/godot › anyone know how to make a float display up to 2 decimal points?
r/godot on Reddit: Anyone know how to make a float display up to 2 decimal points?
February 1, 2022 -

I have a float variable that only goes to 2 decimal points and when I cast it as a string to a text field it would only show 1 or none if the rest are 0. For example 10.90 would be 10.9 and 1.00 would be just 1.

Edit: sorry I said it in a very confusing way. This is what currently happens.

var total = 10.90

text = str(total)

the text field would be 10.9

I’m looking for a way for it to show 10.90 I want it to always be on 2 decimal places. Hope this clarifies things.

Thanks for the help.

🌐
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 - One issue you might face is that printf prints unexpected values when dealing with doubles. Make sure you use the %f format specifier for double-precision floating-point numbers. Using the wrong specifier can lead to incorrect output.