在 C 中舍入整数有问题
Having problems with rounding integers in C
我有一个练习告诉我这些:
函数
问题 1
函数 floor
可用于将数字四舍五入到特定的小数位。语句
y = floor( x * 10 + .5 ) / 10;
将 x 舍入到十分位(小数点右侧的第一个位置)。这
声明
y = floor( x * 100 + .5 ) / 100;
将x四舍五入到百分之一的位置(小数点右边第二个位置
点)。
编写一个程序,定义四个函数以各种方式舍入数字 x
一个。 roundToInteger( 数字 )
b. roundToTenths( 数 )
C。 roundToHundreths( 数字 )
d. roundToThousandths( 数字 )
对于读取的每个值,您的程序应该打印原始值,四舍五入到
最接近的整数,四舍五入到最接近的十分之一的数字,四舍五入到
最接近的百分之一,数字四舍五入到最接近的千分之一。
输入格式
输入行包含一个浮点数。
输出格式
打印原始值,四舍五入到最接近整数的数字,四舍五入的数字
到最接近的十分之一,数字四舍五入到最接近的百分位,以及数字
四舍五入到最接近的千分之一
示例:
输入
24567.8
输出
24567.8 24568 24570 24600
我的解决方案(这是错误的)是:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
double roundToInteger(double number)
{
double roundedNum;
roundedNum = floor(number + .5);
return roundedNum;
}
double roundToTenths(double number)
{
double roundedNum;
roundedNum = floor(number * 10.0 + .5) / 10.0;
return roundedNum;
}
double roundToHundreths(double number)
{
double roundedNum;
roundedNum = floor(number * 100.0 + .5) / 100.0;
return roundedNum;
}
double roundToThousandths(double number)
{
double roundedNum;
roundedNum = floor(number * 1000.0 + .5) / 1000.0;
return roundedNum;
}
int main()
{
double userInput = 0.0, userInput1 = 0.0, userInput2 = 0.0,userInput3 = 0.0, userInput4 = 0.0, originalVal = 0.0;
printf("Enter a double value: ");
scanf("%lf", &userInput);
originalVal = userInput;
userInput1 = roundToInteger(userInput);
userInput2 = roundToTenths(userInput);
userInput3 = roundToHundreths(userInput);
userInput4 = roundToThousandths(userInput);
printf("%lf %lf %lf %lf %lf", originalVal, userInput1,userInput2,userInput3, userInput4);
}
我在公式中做错了什么?
如果输入为 24567.8
并且预期输出为 24567.8 24568 24570 24600
,请将公式更改为:
y = floor( x * 10...0 + .5 ) / 10...0;
收件人:
y = floor( x / 10...0 + .5 ) * 10...0;
换句话说,您正确地实现了所提供的公式。这里唯一的错误是公式对于 10 的四舍五入是错误的。这些是四舍五入的小数位。
我有一个练习告诉我这些:
函数
问题 1
函数 floor
可用于将数字四舍五入到特定的小数位。语句
y = floor( x * 10 + .5 ) / 10;
将 x 舍入到十分位(小数点右侧的第一个位置)。这 声明
y = floor( x * 100 + .5 ) / 100;
将x四舍五入到百分之一的位置(小数点右边第二个位置 点)。
编写一个程序,定义四个函数以各种方式舍入数字 x
一个。 roundToInteger( 数字 )
b. roundToTenths( 数 )
C。 roundToHundreths( 数字 )
d. roundToThousandths( 数字 )
对于读取的每个值,您的程序应该打印原始值,四舍五入到 最接近的整数,四舍五入到最接近的十分之一的数字,四舍五入到 最接近的百分之一,数字四舍五入到最接近的千分之一。
输入格式
输入行包含一个浮点数。
输出格式
打印原始值,四舍五入到最接近整数的数字,四舍五入的数字
到最接近的十分之一,数字四舍五入到最接近的百分位,以及数字
四舍五入到最接近的千分之一
示例:
输入
24567.8
输出
24567.8 24568 24570 24600
我的解决方案(这是错误的)是:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
double roundToInteger(double number)
{
double roundedNum;
roundedNum = floor(number + .5);
return roundedNum;
}
double roundToTenths(double number)
{
double roundedNum;
roundedNum = floor(number * 10.0 + .5) / 10.0;
return roundedNum;
}
double roundToHundreths(double number)
{
double roundedNum;
roundedNum = floor(number * 100.0 + .5) / 100.0;
return roundedNum;
}
double roundToThousandths(double number)
{
double roundedNum;
roundedNum = floor(number * 1000.0 + .5) / 1000.0;
return roundedNum;
}
int main()
{
double userInput = 0.0, userInput1 = 0.0, userInput2 = 0.0,userInput3 = 0.0, userInput4 = 0.0, originalVal = 0.0;
printf("Enter a double value: ");
scanf("%lf", &userInput);
originalVal = userInput;
userInput1 = roundToInteger(userInput);
userInput2 = roundToTenths(userInput);
userInput3 = roundToHundreths(userInput);
userInput4 = roundToThousandths(userInput);
printf("%lf %lf %lf %lf %lf", originalVal, userInput1,userInput2,userInput3, userInput4);
}
我在公式中做错了什么?
如果输入为 24567.8
并且预期输出为 24567.8 24568 24570 24600
,请将公式更改为:
y = floor( x * 10...0 + .5 ) / 10...0;
收件人:
y = floor( x / 10...0 + .5 ) * 10...0;
换句话说,您正确地实现了所提供的公式。这里唯一的错误是公式对于 10 的四舍五入是错误的。这些是四舍五入的小数位。