目录

  • 一.round 函数简介
  • 二.round 函数使用
  • 三.猜你喜欢

零基础 C/C++ 学习路线推荐 : C/C++ 学习目录 >> C 语言基础入门

零基础 C/C++ 学习路线推荐 : C/C++ 学习目录 >> C++ 面向对象

零基础 C/C++ 学习路线推荐 : C/C++ 学习目录 >> C++ 设计模式

零基础 C/C++ 学习路线推荐 : C/C++ 学习目录 >> C++ STL

零基础 C/C++ 学习路线推荐 : C/C++ 学习目录 >> C/C++ 技术杂谈

零基础 C/C++ 学习路线推荐 : C/C++ 学习目录 >> C/C++ 常用函数

一.round 函数简介

在 C 语言中 round 函数用于对浮点数 float 或者 double 或者 longdouble 四舍五入,也是一个比较常用的函数 ,语法如下:

#include <math.h> //需要包含头文件

extern float roundf(float);//参数为flot类型
extern double round(double);//参数为double类型
extern long double roundl(long double);//参数为long double类型

注意:round函数的返回是double类型,并非int` 类型;

二.round 函数使用

round 函数主要用于对浮点数四舍五入,示例如下:

/******************************************************************************************/
//@Author:猿说编程
//@Blog(个人博客地址): www.codersrc
//@File:C/C++ round 函数
//@Time:2021/08/24 08:00
//@Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!
/******************************************************************************************/

#include "stdafx.h"
#include "stdio.h"
#include "stdlib.h"
#include "windows.h"
#include <math.h>
int main()
{
    printf("round(50.2) = %f \n", round(50.2));
    printf("round(50.8) = %f \n", round(50.8));
    printf("round(0.2) = %f \n", round(0.2));
    printf("round(-50.2) = %f \n", round(-50.2));
    printf("round(-50.8) = %f \n", round(-50.8));
    printf("round(100.2) = %f \n", round(100.2));
    system("pause");
    return 0;
}
/*
输出:
round(50.2) = 50.000000
round(50.8) = 51.000000
round(0.2) = 0.000000
round(-50.2) = -50.000000
round(-50.8) = -51.000000
round(100.2) = 100.000000
Press any key to continue . . .
*/

三.猜你喜欢

  1. C 语言 数组下标越界和内存溢出区别
  2. C 语言 使用指针遍历数组
  3. C 语言 指针和数组区别
  4. C 语言 指针数组和数组指针区别
  5. C 语言 野指针
  6. C 语言 函数值传递和址传递
  7. C 语言 函数不定长参数
  8. C 语言 函数指针
  9. C 语言 指针函数
  10. C 语言 回调函数 callback
  11. C 语言 #pragma once
  12. C 语言 #include <> 与 #include “” 区别
  13. C 语言 const 修饰函数参数
  14. C 语言 const 和 define 区别
  15. C 语言 #运算符
  16. C 语言 ##运算符
  17. C 语言 __VA_ARGS__
  18. C 语言 ##__VA_ARGS__
  19. C 语言 函数不定长参数 ##__VA_ARGS__经典案例
  20. C 语言 va_start / va_end / va_arg 自定义 printf 函数
  21. C 语言 main 函数
  22. C 语言 main 函数参数 main(int argc, char *argv[])
  23. C 语言 局部变量
  24. C 语言 全局变量
  25. C 语言 全局变量和局部变量区别
  26. C 语言 static
  27. C 语言 extern
  28. C/C++ Unicode 和多字节区别
  29. C/C++ wprintf 输出中文乱码
  30. C/C++ char 和 wchar_t 相互转换

未经允许不得转载:猿说编程 » C/C++ round 函数

更多推荐

C/C++ round 函数 - C语言零基础入门教程