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
๐ŸŒ
W3Schools
w3schools.com โ€บ c โ€บ c_data_types_dec.php
C Data Types Decimal Precision
Control how many decimal places a float or double shows when printed in C.
๐ŸŒ
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 - In this code snippet, we set the precision to two decimal places by using %.2f. The result will be โ€œMy double value is: 3.14.โ€ Adjust the value of <n> as needed to control the level of precision in your printed double values.
Discussions

formatting - C language how to format a double to 2 digits after the decimal point? - Stack Overflow
Possible Duplicate: Limit floating point precision? In C language, I need to format a number to 2 digits after the decimal point from user input For example: float x ; printf("Enter numbe... More on stackoverflow.com
๐ŸŒ stackoverflow.com
c# - Formatting a double to two decimal places - Stack Overflow
This will format answer as currency, so on my machine (UK) it will come out as: ... Save this answer. ... Show activity on this post. The problem is that when you are doing additions and multiplications of numbers all with two decimal places, you expect there will be no rounding errors, but remember the internal representation of double is in base 2... More on stackoverflow.com
๐ŸŒ stackoverflow.com
c# - Formatting a double to two decimal places - Stack Overflow
I need the doubles to always stay at two decimal places but the answer will not reflect this. so a format of: 209.00 (if you chose 1 product for product three and zero for all others this should be the answer.) ... I was getting 108.9955, but now with Damith's code it works. ... Well, depending on your needs you can choose any of the following. Out put is written against each method ... This will ensure that 2 ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
[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
๐ŸŒ
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 - There are multiple ways to round a double to 2 decimal places in C, including using the โ€œ%.2fโ€ format specifier from printf function, the floor and ceil functions, and the round function.
๐ŸŒ
Uncodemy
uncodemy.com โ€บ blog โ€บ understanding-format-specifier-of-double-in-c
Understanding Format Specifier of Double in C
September 23, 2026 - It wonโ€™t work correctly. Always use %lf when using scanf() with a double. Without it, the program wonโ€™t know where to store the input. If you want your numbers to look clean (like two decimal places), always specify it using %.2lf or similar.
Find elsewhere
๐ŸŒ
Coder Legion
coderlegion.com โ€บ 9697 โ€บ how-to-round-off-double-value-to-2-decimal-places-in-c
How to round off double value to 2 decimal places in C++ - Coder Legion
January 14, 2026 - Preciseness is crucial in programming, particularly when working with numerical quantities. It's common to need to round a double to a certain number of decimal places in a variety of fields, from scientific calculation to banking. We will look into...
๐ŸŒ
Reddit
reddit.com โ€บ r/cpp_questions โ€บ storing a value as two digits after decimal
r/cpp_questions on Reddit: Storing a value as two digits after decimal
June 20, 2014 -

I can't figure out the code to take a double f = 44.444 and store it into double c as 44.44. I need to make the precision after the decimal place 2.

For cout << setprecision(2) << fixed << f, I know this works, but I don't want to print on the screen.

๐ŸŒ
IronPDF
ironpdf.com โ€บ ironpdf blog โ€บ .net help โ€บ csharp-round-to-2-decimal-places
C# Rounding to 2 Decimal Places (Beginner-Friendly Guide)
August 2, 2026 - You can also use this math function to round double values to the nearest integer. This Math.Round method is versatile as it allows you to specify how many digits you want to retain after the decimal point, and it also lets you choose the rounding strategy if the number is exactly halfway between two intervals. To round a decimal number to two decimal places, you can use the Math.Round method that takes two parameters: the number to be rounded and the number of decimal places.
๐ŸŒ
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; }
๐ŸŒ
Microsoft Learn
learn.microsoft.com โ€บ en-us โ€บ dotnet โ€บ standard โ€บ base-types โ€บ standard-numeric-format-strings
Standard numeric format strings - .NET | Microsoft Learn
The precision specifier indicates the desired number of decimal places. If the precision specifier is omitted, the current NumberFormatInfo.NumberDecimalDigits property supplies the numeric precision. The result string is affected by the formatting information of the current NumberFormatInfo object. The following table lists the properties of the NumberFormatInfo object that control the formatting of the result string. The following example formats a Double and an Int32 value with the fixed-point format specifier:
๐ŸŒ
Reddit
reddit.com โ€บ r/cpp_questions โ€บ how to convert a double to a string, truncating it to only two decimals? [c++]
r/cpp_questions on Reddit: How to convert a double to a string, truncating it to only two decimals? [C++]
March 17, 2020 -

The result of the following function: calculateFinalCost() is a double

Currently, I'm using this code:

std::string toStringConsult()
{    	    
	return "\nFinal Cost: " + "$" + std::to_string(calculateFinalCost());
}

However I need toStringConsult() to show the value of calculateFinalCost() truncated from, for example, 45.123321 to 45.12

What would be the simplest way to achieve this?

Thanks