To round a double up to 2 decimal places, you can use:

Use with AI#include <iostream>
#include <cmath>

int main() {
    double value = 0.123;
    value = std::ceil(value * 100.0) / 100.0;
    std::cout << value << std::endl; // prints 0.13
    return 0;
}

To round up to n decimal places, you can use:

Use with AIdouble round_up(double value, int decimal_places) {
    const double multiplier = std::pow(10.0, decimal_places);
    return std::ceil(value * multiplier) / multiplier;
}

This method won't be particularly fast, if performance becomes an issue you may need another solution.

Answer from kaveish on Stack Overflow
๐ŸŒ
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 - You can use these functions by ... to 2 decimal places in C, including using the โ€œ%.2fโ€ format specifier from printf function, the floor and ceil functions, and the round function....
Discussions

Round double in two decimal places in C#? - Stack Overflow
I want to round up double value in two decimal places in c# how can i do that? double inputValue = 48.485; after round up inputValue = 48.49; Related: c# - How do I round a decimal value to 2 dec... 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. ... Save this answer. ... Show activity on this post. @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() Copy#include double ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
January 30, 2014
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
c# - Round Double To Two Decimal Places - Stack Overflow
Possible Duplicate: c# - How do I round a decimal value to 2 decimal places (for output on a page) What is the best way to round a double to two decimal places and also have it fixed at 2 decimal More on stackoverflow.com
๐ŸŒ stackoverflow.com
People also ask

What is the most straightforward method to round numbers in C# to two decimal places?
The Math.Round method in C# is a straightforward way to round numbers to two decimal places by specifying the number to be rounded and the desired number of decimal places.
๐ŸŒ
ironpdf.com
ironpdf.com โ€บ ironpdf blog โ€บ .net help โ€บ csharp-round-to-2-decimal-places
C# Rounding to 2 Decimal Places (Beginner-Friendly Guide)
What are the differences between decimal and double types in C#?
In C#, decimal is used for high precision calculations, often in financial contexts, while double is used for general floating-point calculations where performance is prioritized over precision.
๐ŸŒ
ironpdf.com
ironpdf.com โ€บ ironpdf blog โ€บ .net help โ€บ csharp-round-to-2-decimal-places
C# Rounding to 2 Decimal Places (Beginner-Friendly Guide)
How does C# handle rounding when a number is at the midpoint?
C# allows developers to specify the rounding behavior when a number is at a midpoint through the MidpointRounding enum, such as using MidpointRounding.AwayFromZero to round up midpoints.
๐ŸŒ
ironpdf.com
ironpdf.com โ€บ ironpdf blog โ€บ .net help โ€บ csharp-round-to-2-decimal-places
C# Rounding to 2 Decimal Places (Beginner-Friendly Guide)
๐ŸŒ
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 - The second argument for Math.Round specifies that the rounding should be done to two decimal places. One important aspect of rounding is how to handle cases where the number is exactly at the midpoint between two possible rounded values.
Find elsewhere
๐ŸŒ
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; }
๐ŸŒ
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.
๐ŸŒ
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...
๐ŸŒ
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 ...
๐ŸŒ
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
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%.