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
Discussions

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 do I round a double to one decimal?
Multiply by 10, round, divide by 10. It's worth noting that some decimal values can't be represented by a double. More on reddit.com
๐ŸŒ r/cpp_questions
18
5
October 9, 2023
rounding - C++ round a double up to 2 decimal places - Stack Overflow
I am having trouble rounding a GPA double to 2 decimal places. (ex of a GPA needed to be rounded: 3.67924) I am currently using ceil to round up, but it currently outputs it as a whole number (368)... More on stackoverflow.com
๐ŸŒ stackoverflow.com
floating point - Can you round a value to 2 decimal places in C? - Stack Overflow
I was wondering if I can reduce the value to only 2 decimal places. The resulting number for the volume is from 5 different variables, which I do not know if it matters in this situation. ... @Rashmi solution provides a nicely rounded display of a floating point value. It does not change the value of the original number. If one wants to round a floating point value to the nearest 0.01 use round() #include double ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
January 30, 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 <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; }
๐ŸŒ
Cplusplus
cplusplus.com โ€บ forum โ€บ beginner โ€บ 222475
Rounding offto 2 decimal points - C++ Forum
For rounding methods, check out http://www.cplusplus.com/forum/articles/3638/ (and its follow-up http://www.cplusplus.com/articles/213hAqkS/). Whew. Hope this helps! ... 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
๐ŸŒ
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. ... There might be a more elegant way of doing this in C, but you are posting C++ code here in the C forum so I have an idea that might work on both: ... 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.
๐ŸŒ
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 ...
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.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ cplusplus-program-to-round-a-number-to-n-decimal-places
C++ Program to Round a Number to n Decimal Places
In this article, we shall cover a few techniques to represent a floating-point number up to a certain decimal placed by rounding it off. Among different methods, using C like formatting strings, using precision parameters, and using the round() function present in the math library is important. ...
๐ŸŒ
Reddit
reddit.com โ€บ r/c_programming โ€บ rounding in c
r/C_Programming on Reddit: Rounding in C
May 8, 2024 -

I have a question when it comes to rounding in C. Does it round up or down at .5? If it does round up, then does that mean that the smallest value of k in the code below can only be 1?

 int main()
{
    int k = 13;
    int i;
    for (i = 0; i < 8; i++) {
        printf("%d", (k%2));
        k >>= 1;
    }
    printf("%n");
}

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%.

๐ŸŒ
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....
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ c round() function
C round() Function - Scaler Topics
March 27, 2024 - The round() function in C returns the nearest integer value (rounded off value) of the number provided as an argument which can be of type float, integer, or double number provided as an argument.
๐ŸŒ
Qt Forum
forum.qt.io โ€บ home โ€บ qt development โ€บ general and desktop โ€บ rounding a real number to nth place of decimal
Rounding a real number to nth place of decimal | Qt Forum
March 2, 2022 - And even then it may not be ... ... double roundToNthPlace(double value, int n){ if (n < 1) return static_cast<double>(qRound64(value)); value *= 10 * n; value = qRound(value); value /= 10 * n; return value; }...