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
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
How to round float up to two decimals, inside a variable, in C lang?
This is not something you fix, this is something you understand. Floating point numbers are stored in binary, and not all decimal fractions can be exactly represented as binary fractions. 4.2 is one that can't. This is a fact of life and one of the downsides of using floating point. More on reddit.com
๐ŸŒ r/learnprogramming
15
1
March 27, 2018
๐ŸŒ
W3Schools
w3schools.com โ€บ c โ€บ c_data_types_dec.php
C Data Types Decimal Precision
float myFloatNum = 3.5; printf("%f\n", myFloatNum); // Default will show 6 digits after the decimal point printf("%.1f\n", myFloatNum); // Only show 1 digit printf("%.2f\n", myFloatNum); // Only show 2 digits printf("%.4f", myFloatNum); // Only show 4 digits Try it Yourself ยป ยท When you set the precision, printf() does not just cut off the extra digits. It rounds the number to the nearest value:
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%.

๐ŸŒ
HPE
support.hpe.com โ€บ hpesc โ€บ public โ€บ docDisplay
C: How to round off to the nearest two decimal places
September 8, 2019 - FIX:Using the floor maths function to compute the largest integer not greater than its argument, you can use the following formula to round off to two decimal places: double answer = floor(expression * 100.0 + 0.5) / 100.0 example: if expression ...
Find elsewhere
๐ŸŒ
cyberangles
cyberangles.org โ€บ blog โ€บ rounding-floating-point-number-to-two-decimal-places-in-c-and-c
Rounding Floating Point Numbers to Two Decimal Places in C and C++ โ€” CyberAngles.org
For example, the decimal value 0.1 cannot be represented exactly in binary floating-point format. This leads to precision errors that can accumulate in calculations. #include <stdio.h> int main() { float f = 0.1f; printf("0.1 stored as float: %.20f\n", f); return 0; } ... The simplest approach for display purposes is to use formatted output functions. ... #include <stdio.h> void round_printf(float number) { printf("Original: %f, Rounded: %.2f\n", number, number); } void round_sprintf(float number, char* buffer) { sprintf(buffer, "%.2f", number); }
๐ŸŒ
Cprogramming
cboard.cprogramming.com โ€บ forum index โ€บ general programming boards โ€บ c programming โ€บ round float to 2 decimal places
Round float to 2 decimal places โ€” C Programming โ€” C Board
September 14, 2009 - 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. They can accumulate rounding error and lose cents here and there. Other languages such as VB have a currency ...
๐ŸŒ
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.

๐ŸŒ
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 ...
Top answer
1 of 1
1

... round a float value, say val to its nearest multiple of 0.05 ...

Given typical binary floating point, the best that can be had is

... round a float value to nearest to a multiple of 0.05 R and save as the nearest representable float r.


Method #1 is unclear in the corner cases. What OP has is not code, but an outline of code from a math perspective and not an actual C code one. I'd go with a variation on #2 that works very well. @Barmar

Almost method 2: Multiply, round, then divide.

#include <math.h>
double factor = 20.0;
float val = foo();
double_or_float rounded_val = round(val * factor)/factor;

This method has two subtle points that make it superior.

  1. The multiplication is done with greater precision and range than the referenced answer - this allows for an exact product and a very precise quotient. If the product/quotient were calculated with only float math, some edge cases would end up with the wrong answer and of course some large values would overflow to infinity.

  2. "Ties are settled by taking lower value." is a tough and unusual goal. Sounds like a goal geared to skew selection. round(double) nicely rounds half way cases away from zero regardless of the current rounding direction. To accomplish "lower", change the current rounding direction and use rint() or nearbyint().

    #include <fenv.h>
    int current_rounding_direction = fegetround();
    fesetround(FE_DOWNWARD);
    double rounded_val = rint(val * factor)/factor;
    fesetround(current_rounding_direction);
    

... method-2 yields wrong results some times...

OP needs to post the code and the exact values used and calculated for a quality explanation of the strength/weakness of various methods. Try printf("%a %a\n", val, rounded_val);. Often, problems occurs due to imprecise understanding of the exact values used should code use printf("%f\n", val);


Further: "I already have upper and lower bounds of val, say valU and valL respectively. I can do this in the following ways:"

This is doubtfully accurate as the deviation of valU and valL is just an iteration of the original problem - to find rounded_val. The code to find valL and valU each needs an upper and lower bound, else what is to prevent range [valL ... valU] from itself having inaccurate endpoints?

๐ŸŒ
Unity
discussions.unity.com โ€บ archived forums โ€บ questions & answers
round float with 2 decimal - Questions & Answers - Unity Discussions
September 12, 2013 - float val = num1/num2; The result of val is 2.7456 I need result as 2.75 How can i get it
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ c round() function
C round() Function - Scaler Topics
March 27, 2024 - The C round() function is one of the mathematical functions which is found in <math.h> module of the C library. The round() function in C returns the nearest integer value (rounded value) of the given float, integer, or double number based on the decimal part of the number.
๐ŸŒ
Cplusplus
cplusplus.com โ€บ forum โ€บ beginner โ€บ 222475
Rounding offto 2 decimal points - C++ Forum
If all you want to do is print a number that is half-up rounded at some arbitrary fractional 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 a conflict when dealing with exact digit place value, and (2) FP numbers sample a range of values in a discrete space; they are not continuous.
Top answer
1 of 4
42

I know this is old post but just in case someone else is looking for a quick Two step option.

float old = -3.04299553323;  
float new = [[NSString stringWithFormat:@"%.2f",old]floatValue];

Result = -3.04

The @"%.2f" will round to two decimal places. If you want three decimal places put @"%.3f" and so on.

Hope this helps!

2 of 4
31

You should only ever do this while formatting a number for display to the end user, because there is no guarantee that

float rounded = roundf(orig * 100) / 100.0;

or similar will return an exact answer.

Indeed, in general, it won't. For instance consider

float f = 123456.3;
float r = roundf(f * 100) / 100.0;

printf("%.2f, %.10f\n", r, r);

which outputs

123456.30, 123456.2968750000

Oops!

Using double rather than float helps, but even so, if we change the code a little, you can see that it doesn't really solve the problem:

double f = 123456.3;
double r = round(f * 100) / 100.0;

printf("%.2f, %.20f\n", r, r);

Again, we get

123456.30, 123456.30000000000291038305

which shows quite clearly that it isn't exactly rounded.

Anyway, the moral of the story is that doing things like round(f * 100) / 100.0 only rounds approximately. It might be good enough in some cases, but you do need to keep in mind that the result is not really rounded.

If you want something better, you'll need to use decimal arithmetic instead. You can either do this by keeping your values as integers (e.g. for currency values, keep them in pence or cents instead of pounds or dollars), or by using one of the various decimal floating point packages you can find on Wikipedia's Decimal Floating Point page.

๐ŸŒ
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 - Dim num As Double = 3.14519 ' Round the double value to two decimal places Dim result As Double = Math.Round(num, 2) Console.WriteLine(result) ' Output: 3.15 ยท Dim num As Double = 3.14519 ' Round the double value to two decimal places Dim result ...