I am trying to separate the numbers in a floating input (xx.yy) and extract them out. Not so sure if this is the online compiler fault but for some reasons, there are times when I run my following code, the decimal places that I extract seemingly -1 in value.
I am using this online compiler (https://repl.it/languages/c).
#include <stdio.h>
int main()
{
int whole_num = 0;
int dec_num = 0;
float u_input = 0.0;
printf("Input in a float: ");
scanf("%f", &u_input);
whole_num = (int) u_input;
printf("1. Whole number is %d\n", whole_num);
dec_num = (u_input - whole_num)*100;
printf("2. Decimal number is %d\n", dec_num);
return 0;
}
For example, there are times when I input in 99.95, instead of expecting 95
for my dec_num
, it gave me 94
. Then again, if I used another input such as 123.95
it returns me 95
instead. And as a result, if my code is giving me the -1 result, it will fails in other parts of my code.Decimal
I can't find out if it is an error with the way I have written my code and/ or if it is an issue with the compiler (also tried it with other online compilers which seemingly giving me similar results)
Can anyone share any insights to me on this? Or if there is a proper method that I will get the decimal number I expected without the -1?