Thứ Tư, 27 Tháng Mười Một 2024
Trang chủTranslateC/C++ TutorialsRound and display 2 decimal places in C and C++

Round and display 2 decimal places in C and C++

swapping of two numbers in c, In this article, we will learn how to round decimal numbers and display only 2 decimal places in C and C++ programming.

Rounding Decimal Numbers

Rounding to the Nearest Integer

In C or C++, to round a decimal number, we can use the round() function from the math.h library. If you want to differentiate between pure C and C++ in your program, use #include <math.h> for C and #include <cmath> for C++.

Here’s an example of rounding numbers:

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

int main() {
    float a = 5.654;
    int a1 = round(a);
    printf("Rounding number %f results is: %d", a, a1);

    float b = 5.454;
    int b1 = round(b);
    printf("\nRounded number %f results is: %d", b, b1);

    return 0;
}

Result:

Round and display 2 decimal places in C and C++

In programming, there will often be cases where you don’t round according to mathematical rules but instead round up or down for a specific purpose. The math library provides two functions for that:

  • double ceil(double); => This function always rounds up.
  • double floor(double); => Conversely, this function always rounds down.

Here’s a specific example:

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

int main() {
    float a = 5.9999;
    int a1 = ceil(a);
    int a2 = floor(a);
    printf("Rounded number %f using ceil: %d", a, a1);
    printf("\nRounded number %f using floor: %d", a, a2);

    float b = 9.11111;
    int b1 = ceil(b);
    int b2 = floor(b);
    printf("\n\nRounded number %f using ceil: %d", b, b1);
    printf("\nRounded number %f using floor: %d", b, b2);

    return 0;
}

Result when running the program:

Round and display 2 decimal places in C and C++

To better understand these functions, it’s best to run the program yourself.

Rounding to the nth Decimal Place

To round to a specific decimal place, you can multiply the number by 10^n, round it, and then divide it by 10^n. Here’s an example rounding 9.127658 to the second decimal place:

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

int main() {
    float a = 9.127658;
    float a1 = (round(a * 100)) / 100;
    printf("Rounded number %f to the second decimal place: %f", a, a1);

    return 0;
}

Result:

Round and display 2 decimal places in C and C++

However, in C, numbers are typically displayed with 6 decimal places by default, even if the trailing digits are 0. To display only 2 decimal places, you can change the format string in printf to %.2f.

For C++:

In C++, you can use setprecision(2) and fixed from the iomanip library to achieve the same result. The setprecision(2) function will also round the number to the desired decimal place.

Here’s an example:

#include <iostream>
#include <iomanip>
using namespace std;

int main() {
    float a = 9.127658;
    cout << setprecision(2) << fixed << "a = " << a << endl;
}

Result when running the program:

Round and display 2 decimal places in C and C++

Thank you for reading! Good luck with your studies!

0 0 Phiếu bình chọn
Xếp hạng bài viết
BÀI VIẾT LIÊN QUAN
Đăng ký nhận thông báo
Thông báo email khi
guest
0 Bình luận
Không thể gửi email
Phản hồi nội tuyến

NÊN ĐỌC THÊM

Bạn muốn tìm kiếm gì?

Dịch vụ code thuê

TUICOCACH.COM NHẬN ĐẶT TEXTLINK, BANNER, GP
0
Giáo sư! có thể ném gạch bên dưới nhé!x