Here's what I'm trying to do:
- I need to print the fractional part of a floating number which has to be input as a float during user input.
- The fractional part should be like: if float is 43.3423, the output should be 3423; and if number is 45.3400 output should be 3400.
- This can be done easily with a string input but I need a way to make this work with
floatwithout losing the extra zeros or without appending zeros to user's original input.
Here's what I already tried :-
- Take the fractional part by
frac = num - (int)numand then multiplyingfracuntil we get zero as the remainder. But this fails for cases like 34.3400 — the last two zeros won't get included with this method. Convert the float number to a string by
char string[20]; sprintf(string, "%f", float_number);
The sprintf function puts the float number as a string but here also it doesn't automatically detect the user entered precision and fills the string with extra zeros at the end (6 total precision). So here also the information about the user's original entered precision is not obtained.
So, is there a way to get this done? The number must be taken as float number from user. Is there any way to get info about what's the user's entered precision? If it's not possible, an explanation would be very helpful.
float. In short, without using strings as input it's simply not possible.atof()orstrtod()), and using the counted number for output formatting. It sounds like quite much effort (a lot of character fiddling and many places where error checking should be done). I would think twice about whether the "pretty looking output" is worth the implementation effort...