删除c++中小数点后的数字(不带floor()函数)
Remove numbers after decimal point in c++ (without floor() function)
如果不使用floor()
函数,我将如何复制下面的代码?我需要这样做,因为我的职位不能使用floor()
功能
double quickExampleee = 3.1459038585;
std::cout << std::floor(quickExamplee * 100.) / 100. << std::endl;
我到处找答案,却什么都找不到?无论如何,感谢您抽出时间,
如果您不喜欢将数字强制转换为int,比如,那么使用std::setprecision
怎么样
std::cout << std::setprecision(0) << quickExamplee << std::endl;
所以要打印出3.14,只需将精度设置为2,就像一样
std::cout << std::setprecision(2) << quickExamplee << std::endl;
只要双精度大于0,就可以执行static_cast<int>(quickExampleee * 100.)
。
int myFloorForWhateverReason(double x) {
if (x > 0.0)
return (int)x;
else
return (int)(x - 0.5);
}
double quickExampleee = 3.1459038585;
std::cout << myFloorForWhateverReason(quickExamplee * 100.) / 100. << std::endl;
相关文章:
- 使用不带参数的函数访问结构元素
- f 是指向函数的指针,该函数采用 int,并返回指向不带任何内容并返回双精度的函数的指针
- C++协程:调用不带co_await的协程函数
- sizeof 函数如何在带和不带位字段的结构上工作?(填充)
- 反转不带反转函数的字符串向量
- C++ 创建不带默认构造函数的对象数组
- 静态成员函数赋值而不带返回语句
- 在转换中使用函子(带/不带构造函数)
- 不带初始值设定项的构造函数列表,其中包含带有已删除构造函数的对象
- C++:返回类型为 T 的模板函数不带 T 类型参数,将无法编译
- 警告:函数使用不带尾随返回类型的'auto'类型说明符
- 不带星号的函数指针参数
- 不带成员函数的类实例的默认返回
- 在字符串.h文件中调用函数而不带对象
- 将常量字符* 转换为不带构造函数的 std::string
- 两个构造函数(带和不带参数),没有输入 -> 没有参数运行。跳过上述类中的构造函数
- 不能将类中的输入函数与带参数的函数分开
- 在源代码中,调用函数时不带 ().文档给出了 1 个必需的参数
- 函数不带 0 个参数
- 'itoa':函数不带1个参数&'c':未声明的标识符