[英]Converting a float to a double to an integer
I have to make a project that consists of calculating the amount of coins you need to form a number. 我必须创建一个项目,该项目包括计算形成一个数字所需的硬币数量。 You have quartes, dimes, nickels and pennies and you need to form a number with the least number of coins possible. 您有夸脱,角钱,镍币和几美分,并且您需要形成一个数量最少的硬币。 The number you need to form is prompted to the user. 您需要填写的号码会提示用户。
This is very simple in Python(I implement a function to do something quick): 这在Python中非常简单(我实现了一个函数来快速执行操作):
def calculate_change(change):
count = 0
change*=100
change = int(change)
if change // 25 != 0:
count = change // 25
change = change % 25
if change // 10 != 0:
count += change // 10
change = change % 10
if change // 5 != 0:
count += change // 5
change = change % 5
if change // 1 != 0:
count += change // 1
print count
calculate_change(73)
Now, to implement this in C, I'm having some troubles. 现在,要在C中实现此功能,我遇到了一些麻烦。 I don't quite understand how to change the float the user prompted. 我不太了解如何更改用户提示的浮动。 I need to convert it first to double, multiply by 100 and then convert to an int but I'm having troubles with it. 我需要先将其转换为两倍,再乘以100,然后转换为int,但是我遇到了麻烦。 This is what I wrote for that: 这是我为此写的:
int main(void)
{
float n;
do
{
printf("Change you need: ");
n = GetFloat();
}
while (n < 0);
n = floorf(n * 100 + 0.5) / 100;
int change = (int) n;
change = change * 100;
int count = 0;
if (change / 25 != 0)
{
count = change / 25;
change = change % 25;
}
if (change / 10 != 0)
{
count = count + change / 10;
change = change % 10;
}
if (change / 5 != 0)
{
count = count + change / 5;
change = change % 5;
}
if (change / 1 != 0)
{
count = count + change / 1;
change = change % 1;
}
printf("%d\n", count);
}
Is there something wrong with the conversion from floats to ints in C ? 从C中的float到int的转换有什么问题吗? The program returns incorrect results (always 0) when user prompts a number between 0 and 1. 当用户提示一个介于0和1之间的数字时,程序返回错误的结果(总是0)。
Thanks ! 谢谢 !
Problem 问题
When n = 0.5
, 当n = 0.5
,
floorf(n * 100 + 0.5) / 100 = floorf(50.5)/100 = 50/100 = 0.5
Hence, the statement 因此,声明
n = floorf(n * 100 + 0.5) / 100;
does not change the value of n
. 不会改变n
的值。 Now, when you execute: 现在,当您执行时:
int change = (int) n;
change
is set to 0
. change
设置为0
。 That explains why you get 0
for most values of n
between 0.0-1.0
. 这就解释了为什么对于大多数介于0.0-1.0
之间的n
值获得0
。
Solution 解
I would replace the following lines: 我将替换以下几行:
n = floorf(n * 100 + 0.5) / 100;
int change = (int) n;
change = change * 100;
with 与
n = floorf(n * 100 + 0.5); // Just to make sure rounding occurs properly.
// Thanks to Mark Ransom.
int change = (int) n;
You are right that you need to multiply by 100 and then convert to an int, and that's what you're doing in the python code, but in your C code you are doing it backwards - first converting to an int and then multiplying by 100: 没错,您需要乘以100,然后转换为一个int,这就是您在python代码中所做的,但是在您的C代码中,您正在反向进行-首先转换为int,然后乘以100 :
int change = (int) n;
change = change * 100;
That (int) cast truncates values to the next lowest integer, so values of n between 0 and 1 will always get set to 0. (int)强制将值截断为下一个最小整数,因此n介于0和1之间的值将始终设置为0。
Moving the multiplication by 100 ensures that you convert from dollars (eg 0.56) to a floating point representation of cents (eg 56.0) before converting to an integer of cents eg (56): 将乘数移动100可确保您在转换为整数(例如(56))之前,将美元(例如0.56)转换为美分(例如56.0)的浮点表示形式:
int change = (int) (n * 100);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.