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
🌐
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 <stdio.h> int main() { float var = 37.66666; // Directly print the number with .2f precision printf("%.2f", var); return 0; } ... Time Complexity: O(1), as the execution time does not depend on the size of the input. Space Complexity: O(1), as it only uses a fixed amount of memory to store a single float variable and does not allocate any dynamic memory. Second Method: Using integer typecast If we are in Function then how return two decimal point value ... #include <iostream> using namespace std; float round(float var) { // 37.66666 * 100 =3766.66 // 3766.66 + .5 =3767.16 for rounding off value // then type cast to int so value is 3767 // then divided by 100 so the value converted into 37.67 float value = (int)(var * 100 + .5); return (float)value / 100; } int main() { float var = 37.66666; cout << round(var); return 0; }
Discussions

rounding - Round float to 2 decimal places in C language? - Stack Overflow
float number = 123.8798831; number=(floorf((number + number * 0.1) * 100.0)) / 100.0; printf("number = %f",number); I want to get number = 136.25 But the compiler shows me ... More on stackoverflow.com
🌐 stackoverflow.com
floating point - Can you round a value to 2 decimal places in C? - Stack Overflow
I am calculating the volume of a room and I got a number with 6 decimal places. I was wondering if I can reduce the value to only 2 decimal places. The resulting number for the volume is from 5 dif... More on stackoverflow.com
🌐 stackoverflow.com
January 30, 2014
Rounding offto 2 decimal points - C++ Forum
If all you want to do is print ... place, use the stream manipulators: However, if you want to actually modify the numeric value, you'll need to multiply, round, and divide. The caveat is that this might not do exactly what you think. The reason is that floating point numbers are not exact. And the reason for that is twofold: (1) numbers in the computer are represented in binary but we view them in decimal, which is ... More on cplusplus.com
🌐 cplusplus.com
How to round a float to two decimals? - Programming & Scripting - Epic Developer Community Forums
I want to round a float, but I want it to always have 2 numbers after the dot. even if its say 10.00, so it doesn’t mess the string. problem is that if I multiply by 100, integer and then divide by 100, if the number is .0 it doesn’t show the zeroes, and also after a while, the precision ... More on forums.unrealengine.com
🌐 forums.unrealengine.com
0
October 29, 2019
🌐
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...
🌐
Cprogramming
cboard.cprogramming.com › c-programming › 119573-round-float-2-decimal-places.html
Round float to 2 decimal places
September 14, 2009 - 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.
Top answer
1 of 3
2

It is necessary that the number be stored in a variable as number = 136.25

But that would be the incorrect result. The precise result of number + number * 0.1 is 136.26787141. When you round that downwards to 2 decimal places, the number that you would get is 136.26, and not 136.25.

However, there is no way to store 136.26 in a float because it simply isn't a representable value (on your system). Best you can get is a value that is very close to it. You have successfully produced a floating point number that is very close to 136.26. If you cannot accept the slight error in the value, then you shouldn't be using finite precision floating point arithmetic.

If you wish to print the value of a floating point number up to limited number of decimals, you must understand that not all values can be represented by floating point numbers, and that you must use %.2f to get desired output.

Round float to 2 decimal places in C language?

Just like you did:

  • multiply with 100
  • round
  • divide by 100
2 of 3
1

I agree with the other comments/answers that using floating point numbers for money is usually not a good idea, not all numbers can be stored exactly. Basically, when you use floating point numbers, you sacrifice exactness for being able to storage very large and very small numbers and being able to store decimals. You don't want to sacrifice exactness when dealing with real money, but I think this is a student project, and no actual money is being calculated, so I wrote the small program to show one way of doing this.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main(void)
{
    double number, percent_interest, interest, result, rounded_result;

    number = 123.8798831;
    percent_interest = 0.1;
    interest = (number * percent_interest)/100; //Calculate interest of interest_rate percent.
    result = number + interest;
    rounded_result = floor(result * 100) / 100;


    printf("number=%f, percent_interest=%f, interest=%f, result=%f, rounded_result=%f\n", number, percent_interest, interest, result, rounded_result);

    return EXIT_SUCCESS;
}

As you can see, I use double instead float, because double has more precession and floating point constants are of type double not float. The code in your question should give you a warning because in

float number = 123.8798831;

123.8798831 is of type double and has to be converted to float (possibly losing precession in the process).

You should also notice that my program calculates interest at .1% (like you say you want to do) unlike the code in your question which calculates interest at 10%. Your code multiplies by 0.1 which is 10/100 or 10%.

Find elsewhere
🌐
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.
🌐
Quora
quora.com › How-do-you-round-to-2-decimal-places-in-C
How to round to 2 decimal places in C++ - Quora
Play the best online casino games, slots & live casino games! Unlock VIP bonuses, bet with crypto & win. ... The same way you round to two decimal places in any other context: you add 0.005 to the value.
🌐
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
B. Sc. in Computer Science, University ... floating-point value to the nearest integer. You can multiply or divide by powers of 10 to round to a given number of decimal places....
🌐
Cplusplus
cplusplus.com › forum › beginner › 222475
Rounding offto 2 decimal points - C++ Forum
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
🌐
Java2Blog
java2blog.com › home › c++ › how to round a number to 2 decimal places in c++
How to Round a Number to 2 decimal places in C++ [7 Ways] - Java2Blog
November 23, 2023 - Example: Input: A floating-point number, say 5.82643. Expected Output: The number rounded to 2 decimal places, which would be 5.83 in this case.
🌐
Fresh2Refresh
fresh2refresh.com › home › c programming tutorial › c – arithmetic functions › c – round() function
C round() function | C Arithmetic functions | Fresh2Refresh
September 23, 2020 - round( ) function in C returns the nearest integer value of the float/double/long double argument passed to this function. If decimal value is from ”.1 to .5″, it returns integer value less than the argument.
🌐
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 }
🌐
Scaler
scaler.com › home › topics › c round() function
C round() Function - Scaler Topics
March 27, 2024 - The output, in this case, will be a smaller integer than this number because the decimal value is 0.4, which is less than 0.5, and as we have discussed above that if the decimal value will be less than or equal to 0.5, the round() function will ...
🌐
Unreal Engine
forums.unrealengine.com › development › programming & scripting
How to round a float to two decimals? - Programming & Scripting - Epic Developer Community Forums
October 29, 2019 - I want to round a float, but I want it to always have 2 numbers after the dot. even if its say 10.00, so it doesn’t mess the string. problem is that if I multiply by 100, integer and then divide by 100, if the number is .0 it doesn’t show the zeroes, and also after a while, the precision ...
🌐
Linux Hint
linuxhint.com › cpp-round-floating-point-number-2-decimal-places
Round the Floating-Point Number to Two Decimal Places in C++
August 21, 2023 - The std::setprecision(2) manipulator sets the precision to two decimal places. This means that the subsequent floating-point numbers that are printed on the console have two decimal places. The line std::cout << “Rounded number: ” << digit << std::endl; uses “std::cout” to print the ...
🌐
Reddit
reddit.com › r/learnprogramming › how to round float up to two decimals, inside a variable, in c lang?
r/learnprogramming on Reddit: How to round float up to two decimals, inside a variable, in C lang?
March 27, 2018 -

I'm doing CS50 and they have this get_float function that prompts the user for a float. Let say that I have a variable 'float n = 0;' next, I get 4.2 from the user that gets stored in that variable. When I debug this code I see that the value of 'n' is something like 4.1999999. How can I fix this? I know that if I wanted to print 'n' I would do printf("%.2f", n);, but I need to work with this variable, it need it to be 4.20. I hope you understand what I'm asking here, and please, excuse my bad english.

🌐
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; } ... In C, there ...