数学四舍五入函数与可变数字数
Math round function with variable number of digits
如何使用与excel round函数相同的数学round函数
float round( float arg );
round(number, num_digits)
如果num_digits大于0,则将number四舍五入到指定的小数位数
如果num_digits为0,则将数字四舍五入到最接近的整数
如果num_digits小于0,则将数字四舍五入到小数点左边。
简单算术,
#include <stdio.h>
#include <cmath>
float my_round( float arg, int digits )
{
float retValue = arg * pow(10.0f,(float)digits);
retValue = round(retValue);
return retValue * std::pow(10.0f,(float)-digits);
}
int main()
{
float value = 12.3456789f;
for(int i=-1;i<6;i++)
{
printf("%fn", my_round(value, i));
}
}
应该可以做到这一点,(用g++编译)
输出为:
10.000000
12.000000
12.300000
12.349999
12.346001
12.345699
12.345679
相关文章:
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- 函数在许多数字上转换为基数 1 时减去 2?
- 函数如何使用引用返回所需的数字?
- 当我尝试在 Main 中调用插入函数时,它不是取数字?
- C++ 函数,用于查找传入的 N 个数字的平均值、总和、最小值和最大值
- 如何在 setup() 函数中使用数字引脚转动电机?
- C++ python 中的函数返回数字而不是字符串
- 如何在不使用 C++ 中的数组或函数的情况下查找 N 位数字的所有排列
- 将 C 函数转换为 C++ 以检查数字是否有效
- 在C++中编程,将 3 个数字发送到一个函数,然后计算这 3 个数字的平均函数
- 是否有一个 std::set 函数来确定不超过数字 x 的最大元素?
- 在 c++ 中将数字转换为矢量数组的函数
- 使用带有一个参数函数的递归找到数字的平方
- 我想使用 std::stoi 函数在 cpp 中将字符串转换为整数,因为我想在字符串中找到不同数字的总和(在下级酶中)
- 为什么当我在 c++ 中运行函数时,我的代码显示数字 53
- 我有模板功能.通过使用函数std::for_each,用这个容器中的最大数字替换每个正数
- 如何用不同的参数类型和数字回调函数
- 我必须更改我的数字最后一个数字和第一个数字,但不要使用仅带有整数或循环的函数.例如从 12345 到 52341
- 素数检查 C++ 函数输出非素数的数字
- C++ 在地板函数之后以十六进制打印数字