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
๐ŸŒ
YouTube
youtube.com โ€บ coffee programmer
C Programming Limiting floats to two decimal places - How to limit and display float variable values - YouTube
Can you help me to buy a coffee:https://www.buymeacoffee.com/coffeeprogram how to limit float to 2 decimal places in c
Published ย  October 12, 2022
Views ย  2K
Discussions

Limiting floating numbers to 2 decimal places - Processing 2.x and 3.x Forum
I use text() to place numbers for result on display: text(num,x,y); Floating point numbers display 3 decimal places by default . More on forum.processing.org
๐ŸŒ forum.processing.org
August 14, 2017
rounding - Round float to 2 decimal places in C language? - Stack Overflow
If you wish to print the value ... 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. ... Sign up to request clarification or add additional context in comments. ... I work with money, I need to multiply the current account money by 0.1% each time and round it to 2 decimal places... More on stackoverflow.com
๐ŸŒ stackoverflow.com
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
[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
๐ŸŒ
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<bits/stdc++.h> using namespace std; int main() { float var = 37.66666; // Directly print the number with .2f precision cout << fixed << setprecision(2) << var; return 0; } // This code is contributed by shivanisinghss2110
๐ŸŒ
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
๐ŸŒ
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 ...
๐ŸŒ
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.
Find elsewhere
๐ŸŒ
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.
๐ŸŒ
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 ยป
๐ŸŒ
Processing Forum
forum.processing.org โ€บ two โ€บ discussion โ€บ 23814 โ€บ limiting-floating-numbers-to-2-decimal-places.html
Limiting floating numbers to 2 decimal places - Processing 2.x and 3.x Forum
August 14, 2017 - It works convert to string then use str.indexOf(.) to get number of sig digits to left then nf(strval,strval.indexOf("."),2) to return modified string version of number for text. ... float f1 = 1.2; println(nf(f1, 0, 2)); // 1.20 float f2 = ...
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%.

๐ŸŒ
Linux Hint
linuxhint.com โ€บ setting_decimal_precision_c_-language
Setting Decimal Precision in C โ€“ Linux Hint
The following image shows the compilation and execution of this code. As you can see, the printf() function sets the precision of the float to 1, 2, and 3 decimal digits:
๐ŸŒ
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 // using format specifier #include<stdio.h> int main() { float num = 5.48958123; // 4 digits after the decimal point printf("%0.4f", num); return 0; }
๐ŸŒ
HPE
support.hpe.com โ€บ hpesc โ€บ public โ€บ docDisplay
C: How to round off to the nearest two decimal places
Important Note: Support Center is currently scheduled for planned maintenance between July 1, 2026, 9:30 PM PDT and July 1, 2026, 10:30 PM PDT. During this time, some site features may be temporarily unavailable. We apologize for this inconvenience ยท You have no new notifications
๐ŸŒ
Cprogramming
cboard.cprogramming.com โ€บ c-programming โ€บ 134040-how-take-only-two-decimals-float-without-rounding.html
How to take only two decimals of float without rounding?
My first impulse would be to treat the float variable as a string and just cut away everything after .xx (all decimals after the second) manually. Last edited by cnewbie1; 01-24-2011 at 11:41 AM. ... Look up the floor() function in IIRC math.h; might not do what you want but it or round() with division/multiplication should. Homemade code without math.h, not tested printf("The furniture cost $ %.2f", (int)(x*100)/100.0); Homemade code with math.h, not tested printf("The furniture cost $ %.2f", floor(x*100)/100.0); Tim S.
๐ŸŒ
Java2Blog
java2blog.com โ€บ round-to-2-decimal-places-cpp
How to Round a Number to 2 decimal places in C++ [7 Ways]
November 23, 2023 - This domain name has expired. If you are the registered holder of this name and wish to renew it, please contact your registration service provider ยท java2blog.com ยท Related Search Topics ยท Copyright ยฉ java2blog.com. All rights reserved.Privacy Policy
๐ŸŒ
Reddit
reddit.com โ€บ r/c_programming โ€บ maximum decimal places
r/C_Programming on Reddit: Maximum decimal places
June 10, 2020 -

What is the maximum decimal places that can be stored using c? I tried to store - 1.86264514923107200935514487085e-09 but only got 0.00000000186264514923107200000000.....

Top answer
1 of 5
3
https://en.wikipedia.org/wiki/Double-precision_floating-point_format Significant precision is 53 bits = 9.00719925e15, so that's 15 (almost 16) decimal digits of significance
2 of 5
3
If you care about preserving the value across a complete decimal โ†’ floating-point โ†’ decimal round-trip: #include #include int main(void) { printf("FLT_DIG = %d\n", FLT_DIG); printf("DBL_DIG = %d\n", DBL_DIG); printf("LDBL_DIG = %d\n", LDBL_DIG); } On my system, I get: FLT_DIG = 6 DBL_DIG = 15 LDBL_DIG = 18 Note that this does not mean that all values with at most this many decimal digits can be represented "exactly". It just means that the round-trip will yield the same decimal digits at the end. Note also that "decimal digits" here is not just the digits after the decimal point. 1234.56 has 6 significant decimal digits. If stored in a float and then reformatted as a decimal value, it will yield the same decimal value on my system. But 1234.567 has 7 significant decimal digits, and this is not guaranteed to yield the same decimal value if round-tripped through a float on my system. If you care about a floating-point โ†’ decimal โ†’ floating-point round-trip, you need more decimal digits: #include #include int main(void) { printf("FLT_DECIMAL_DIG = %d\n", FLT_DECIMAL_DIG); printf("DBL_DECIMAL_DIG = %d\n", DBL_DECIMAL_DIG); printf("LDBL_DECIMAL_DIG = %d\n", LDBL_DECIMAL_DIG); } I get: FLT_DECIMAL_DIG = 9 DBL_DECIMAL_DIG = 17 LDBL_DECIMAL_DIG = 21 These values are greater than the ones before because round-tripping in this direction needs to preserve the precision in the original floating-point value.