C语言中double类型round函数如何使用?

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

C 语言标准库 <math.h> 提供了几个相关的函数,最常用的是 round()floor()ceil(),C99 标准还引入了更精确的 roundl()llround() 等函数。

c 语言 double round
(图片来源网络,侵删)

核心函数:round()

round() 函数是进行“四舍五入”最直接、最常用的函数。

函数原型

#include <math.h>
double round(double x);

功能

它将参数 x 的值舍入到最接近的整数。x 恰好在两个整数之间(5),则向“远离零”的方向舍入(即 5 舍入为 0-2.5 舍入为 -3.0)。

示例代码

#include <stdio.h>
#include <math.h> // 必须包含此头文件
int main() {
    double num1 = 3.14;
    double num2 = 2.5;
    double num3 = 2.49;
    double num4 = -2.5;
    double num5 = -2.49;
    double result1 = round(num1);
    double result2 = round(num2);
    double result3 = round(num3);
    double result4 = round(num4);
    double result5 = round(num5);
    printf("round(%.2f) = %.0f\n", num1, result1); // 输出: round(3.14) = 3
    printf("round(%.2f) = %.0f\n", num2, result2); // 输出: round(2.5) = 3
    printf("round(%.2f) = %.0f\n", num3, result3); // 输出: round(2.49) = 2
    printf("round(%.2f) = %.0f\n", num4, result4); // 输出: round(-2.5) = -3
    printf("round(%.2f) = %.0f\n", num5, result5); // 输出: round(-2.49) = -2
    return 0;
}

其他相关舍入函数

除了 round(),根据不同的需求,你可能还会用到以下函数:

函数 功能 示例 (x = 2.8) 示例 (x = 2.3) 示例 (x = -2.8)
round(x) 四舍五入到最近的整数 0 0 -3.0
floor(x) 向下取整(不大于 x 的最大整数) 0 0 -3.0
ceil(x) 向上取整(不小于 x 的最小整数) 0 0 -2.0
trunc(x) 截断小数部分(直接丢弃) 0 0 -2.0

示例代码

#include <stdio.h>
#include <math.h>
int main() {
    double x = 2.8;
    printf("round(%.1f) = %.1f\n", x, round(x)); // 输出: round(2.8) = 3.0
    printf("floor(%.1f) = %.1f\n", x, floor(x)); // 输出: floor(2.8) = 2.0
    printf("ceil(%.1f) = %.1f\n", x, ceil(x));   // 输出: ceil(2.8) = 3.0
    printf("trunc(%.1f) = %.1f\n", x, trunc(x)); // 输出: trunc(2.8) = 2.0
    x = -2.8;
    printf("\nround(%.1f) = %.1f\n", x, round(x)); // 输出: round(-2.8) = -3.0
    printf("floor(%.1f) = %.1f\n", x, floor(x)); // 输出: floor(-2.8) = -3.0
    printf("ceil(%.1f) = %.1f\n", x, ceil(x));   // 输出: ceil(-2.8) = -2.0
    printf("trunc(%.1f) = %.1f\n", x, trunc(x)); // 输出: trunc(-2.8) = -2.0
    return 0;
}

注意: trunc() 函数在 <math.h> 中定义,但一些旧的编译器可能需要额外的编译选项(如 -std=c99)才能识别它。

c 语言 double round
(图片来源网络,侵删)

重要的注意事项

1 链接数学库

在 Linux 或 macOS 环境下编译使用 <math.h> 的代码时,你需要在编译命令末尾加上 -lm,以链接数学库。

# 错误的编译方式 (会报 undefined reference to `round' 等错误)
gcc my_program.c -o my_program
# 正确的编译方式
gcc my_program.c -o my_program -lm

2 返回值类型与精度

  • round() 返回 double 类型,它保留了浮点数的形式,但小数部分为 .0
  • 如果你需要一个整数类型(如 longint),可以使用 lround()llround()
函数 功能 返回类型
round() 四舍五入 double
lround() 四舍五入 long
llround() 四舍五入 long long

示例:

#include <stdio.h>
#include <math.h>
int main() {
    double pi = 3.14159;
    // round() 返回 double
    double rounded_double = round(pi);
    printf("round(pi) = %f (type: double)\n", rounded_double);
    // lround() 返回 long
    long rounded_long = lround(pi);
    printf("lround(pi) = %ld (type: long)\n", rounded_long);
    return 0;
}

3 浮点数精度问题

由于计算机使用二进制表示浮点数,有些十进制小数无法被精确表示,这可能会导致意外的结果。

#include <stdio.h>
#include <math.h>
int main() {
    // 0.1 + 0.2 在二进制中不是一个精确的 0.3
    double sum = 0.1 + 0.2; // sum 可能是 0.30000000000000004
    printf("sum = %.20f\n", sum);
    // 对这个不精确的值进行四舍五入
    double rounded_sum = round(sum * 10.0) / 10.0; // 先放大,四舍五入,再缩小
    printf("rounded_sum = %.1f\n", rounded_sum); // 输出应该是 0.3
    return 0;
}

处理方法:在涉及金融或高精度计算时,如果需要精确到 N 位小数,可以先将数值乘以 10^N,进行 round(),然后再除以 10^N,如上例所示。

c 语言 double round
(图片来源网络,侵删)

如何选择?

你的需求 推荐函数
标准的四舍五入(2.5 变 3, -2.5 变 -3) round()
向下取整(地板,不大于 x 的最大整数) floor()
向上取整(天花板,不小于 x 的最小整数) ceil()
直接去掉小数部分(2.8 变 2, -2.8 变 -2) trunc()
需要整数返回类型 lround() (返回 long) 或 llround() (返回 long long)

对 C 语言中的 double 进行四舍五入,最标准、最直接的方法是使用 <math.h> 库中的 round() 函数,务必记住在编译时链接数学库(-lm),并根据你的具体需求(如返回类型、舍入方向)选择最合适的函数。

-- 展开阅读全文 --
头像
C语言redefinition错误如何解决?
« 上一篇 12-07
C语言如何实现treasure hunt寻宝游戏?
下一篇 » 12-07

相关文章

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

目录[+]