Reputation: 128
How can I make a decimal number to an int. For example, how can I make 0.565 to 565 (Without multiplying by the power of 10) because I wouldn't know how many numbers after decimal point will be there. Because these are user inputs.
Upvotes: 1
Views: 95
Reputation: 70931
This should do:
#include <stdio.h>
#include <math.h>
int main(void)
{
long double ld, ldi;
long long int lli;
{
int n;
do
{
n = scanf("%Lf", &ld);
} while (1 != n);
}
while (LDBL_EPSILON < fabsl(modfl(ld, &ldi))) {
ld *= 10.;
fprintf(stderr, "%.30Lf\n", ld);
}
lli = (long long int) ldi;
printf("%lld\n", lli);
}
if LDBL_EPSILON
is missing try __LDBL_EPSILON __
or alike ...
Upvotes: -1
Reputation: 180286
How can I make a decimal number to an int. For example, how can I make 0.565 to 565 (Without multiplying by the power of 10) because I wouldn't know how many numbers after decimal point will be there. Because these are user inputs.
Accept the inputs as strings. Parse the strings to identify the fractional part of each, and go from there. That's much easier than starting from a float
or double
value, because details of floating-point representation get in the way, such as 0.565 not being exactly representable in binary floating-point.
Upvotes: 3