pete said:
William said:
int main(void)
{
char buffer[100];
// User input?
//
strcpy(&buffer[0], "3.14000100");
puts(removeTralingZeros(&buffer[0]));
getchar();
return 0;
}
This only answers one part of the question
(and that the easy part).
Assume that the floating point number x, represents
the rational 2.19994019993
Should this print out as
2.2
2.1999
2.19994
2.1999402
2.1999401999
or
2.19994019993
This question cannot be answered
by merely looking at x and deciding to
strip trailing zeros. You have to decide which decimal place to
round to.
No your answer showed how to strp trailing zeros from a buffer.
The difficult part of the question is how to convert the
floating point numbers into a string in a buffer.
(Note your example starts with a string, not
a floating point number)
I think it was pretty close.
int main(void)
{
char buffer[100];
sprintf(buffer, "%f", 2.19994019993);
puts(removeTralingZeros(buffer));
return 0;
}
I get 2.19994
I get the same - but I think that's odd, as the way I thought I wrote the
function, it *should* return 2.19994019993, i.e., not trailing zeros to
remove.
However, I get the same result as you.
Upon further investigation, I find that if I puts(buffer) after the sprintf,
I only get 2.199940 as output - instead of 2.1999401993 - and - try as I
might just now, I don't seem to be able to get the full output either, e.g.,
F, lf, all seem to result in sprintf giving the same result.
So, the code seems to work as detailed - it's just that buffer contains
2.199940 when removeTralingZeros gets to look at it.