C语言return rounded如何实现四舍五入?

99ANYc3cd6
预计阅读时长 21 分钟
位置: 首页 C语言 正文

Of course! In C, there isn't a single rounded function, but there are several ways to round numbers, depending on what kind of rounding you need and what data types you're working with.

c语言 return rounded
(图片来源网络,侵删)

Here’s a complete guide covering the most common methods.

Rounding Floating-Point Numbers (float, double)

For floating-point numbers, the standard C library (<math.h>) provides several functions. You must remember to compile with the -lm flag to link the math library.

round()

Rounds a number to the nearest integer. It rounds halfway cases (like .5) away from zero.

  • 4 -> 0
  • 5 -> 0
  • 6 -> 0
  • -1.5 -> -2.0
#include <stdio.h>
#include <math.h> // Required for round()
int main() {
    double num1 = 1.4;
    double num2 = 1.5;
    double num3 = -1.5;
    printf("round(1.4) = %.1f\n", round(num1));   // Output: round(1.4) = 1.0
    printf("round(1.5) = %.1f\n", round(num2));   // Output: round(1.5) = 2.0
    printf("round(-1.5) = %.1f\n", round(num3));  // Output: round(-1.5) = -2.0
    return 0;
}
// To compile: gcc your_file.c -o your_program -lm

floor()

Rounds a number down to the nearest integer. It always rounds towards negative infinity.

c语言 return rounded
(图片来源网络,侵删)
  • 9 -> 0
  • 1 -> 0
  • -1.1 -> -2.0
  • -1.9 -> -2.0
#include <stdio.h>
#include <math.h>
int main() {
    printf("floor(1.9) = %.1f\n", floor(1.9));   // Output: floor(1.9) = 1.0
    printf("floor(-1.1) = %.1f\n", floor(-1.1)); // Output: floor(-1.1) = -2.0
    return 0;
}

ceil() (Ceiling)

Rounds a number up to the nearest integer. It always rounds towards positive infinity.

  • 1 -> 0
  • 9 -> 0
  • -1.1 -> -1.0
  • -1.9 -> -1.0
#include <stdio.h>
#include <math.h>
int main() {
    printf("ceil(1.1) = %.1f\n", ceil(1.1));     // Output: ceil(1.1) = 2.0
    printf("ceil(-1.9) = %.1f\n", ceil(-1.9));   // Output: ceil(-1.9) = -1.0
    return 0;
}

trunc() (Truncate)

Discards the fractional part of the number, effectively rounding towards zero.

  • 9 -> 0
  • 1 -> 0
  • -1.1 -> -1.0
  • -1.9 -> -1.0
#include <stdio.h>
#include <math.h>
int main() {
    printf("trunc(1.9) = %.1f\n", trunc(1.9));   // Output: trunc(1.9) = 1.0
    printf("trunc(-1.9) = %.1f\n", trunc(-1.9)); // Output: trunc(-1.9) = -1.0
    return 0;
}

Rounding to a Specific Number of Decimal Places

This is a very common task. The trick is to multiply the number by a power of 10, round it, and then divide by the same power of 10.

#include <stdio.h>
#include <math.h>
// Function to round a double to a given number of decimal places
double round_to_decimal_places(double value, int decimal_places) {
    double factor = pow(10, decimal_places);
    return round(value * factor) / factor;
}
int main() {
    double price = 19.789;
    // Round to 2 decimal places (for currency)
    double rounded_price = round_to_decimal_places(price, 2);
    printf("Original price: %.6f\n", price);       // Original price: 19.789000
    printf("Rounded price:  %.2f\n", rounded_price); // Rounded price:  19.79
    // Round to 1 decimal place
    double rounded_temp = round_to_decimal_places(98.765, 1);
    printf("Rounded temp:   %.1f\n", rounded_temp); // Rounded temp:   98.8
    return 0;
}

Rounding and Returning an Integer

When you use functions like round(), floor(), etc., on a double or float, they return a floating-point number. If you need an integer (int), you must cast the result.

c语言 return rounded
(图片来源网络,侵删)

Casting to int

This is the most direct way. It implicitly truncates the fractional part (same as trunc()).

#include <stdio.h>
int main() {
    double num = 3.9;
    int result = (int)num; // Cast to int
    printf("(int)3.9 = %d\n", result); // Output: (int)3.9 = 3
    return 0;
}

Casting after Rounding

If you want to perform rounding before converting to an integer, you should call the rounding function first and then cast.

#include <stdio.h>
#include <math.h>
int main() {
    double num = 3.7;
    // 1. Cast directly (truncates)
    int truncated = (int)num;
    printf("(int)3.7 = %d\n", truncated); // Output: (int)3.7 = 3
    // 2. Round first, then cast
    int rounded = (int)round(num);
    printf("(int)round(3.7) = %d\n", rounded); // Output: (int)round(3.7) = 4
    return 0;
}

Summary Table

Function Header Behavior Example (7) Example (-3.7)
round() <math.h> Nearest integer, halves away from zero 0 -4.0
floor() <math.h> Down to the next integer 0 -4.0
ceil() <math.h> Up to the next integer 0 -3.0
trunc() <math.h> Discards fractional part (towards zero) 0 -3.0
(int)x <stdlib.h> Cast to integer (discards fractional part) 3 -3

Important Considerations

  1. -lm Flag: Always remember to link the math library when you use functions from <math.h>. If you get "undefined reference to round" errors, this is the fix.

    gcc your_program.c -o your_program -lm
  2. Integer Division: Be careful with integer division in C. It truncates the result.

    int a = 5 / 2; // a will be 2, not 2.5
  3. Floating-Point Precision: Floating-point numbers are not always represented perfectly. This can sometimes lead to unexpected results. For example, round(2.675 * 100) might not give 0 due to tiny precision errors. For financial applications, it's often better to work in integer cents (e.g., 267 cents) to avoid this.

-- 展开阅读全文 --
头像
C语言switch中selitems是什么?
« 上一篇 2025-11-27
原创dedecms织梦新闻资讯模板【新版】
下一篇 » 2025-11-27

相关文章

取消
微信二维码
支付宝二维码

目录[+]