Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

c++向下取整函数

作者:妳會吥會在吥經意間想起莪?   发布日期:2025-09-10   浏览:4

#include <iostream>
#include <cmath>  // 导入数学函数库

using namespace std;

int main() {
    double num = 4.7;
    int floor_value = floor(num);  // 使用floor函数进行向下取整

    cout << "原始数值: " << num << endl;
    cout << "向下取整后的值: " << floor_value << endl;

    return 0;
}

解释说明:

  1. 导入库#include <cmath> 导入了 C++ 标准库中的数学函数,包括 floor 函数。
  2. 变量定义double num = 4.7; 定义了一个双精度浮点数 num,其值为 4.7。
  3. 向下取整int floor_value = floor(num); 使用 floor 函数对 num 进行向下取整操作,并将结果赋值给 floor_value。注意 floor 返回的是 double 类型,因此这里可以安全地转换为 int
  4. 输出结果:使用 cout 输出原始数值和向下取整后的值。

如果需要处理负数,floor 函数同样适用。例如,floor(-4.7) 的结果是 -5

上一篇:c++ mutex

下一篇:c++ sizeof

大家都在看

c++闭包

c++单引号和双引号的区别

c++ 注释

c++如何判断素数

c++ orm框架

c++freopen怎么用

c++ 获取系统时间

c++进制转换函数

c++ vector 清空

c++ tcp

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站