I wanna take a float with unknown number of decimals and change it into a integer by multiplying it by 10 until it's an integer, and i know it has to be done with a loop but I can't figure out how.
take 216.612 for example, it should be converted to 216612 at the end.
Asked
Active
Viewed 38 times
0

Alireza Mirzaei
- 23
- 7
-
1Does this answer your question (looks like an almost exact duplicate - even using a very similar number)? [printing the double number wrongly](https://stackoverflow.com/questions/65026826/printing-the-double-number-wrongly) – Adrian Mole Dec 01 '20 at 17:48
-
Convert to a string using `sprintf()`, using string techniques remove the decimal point, convert the remaining string back to `int` via `strtol()`. – ryyker Dec 01 '20 at 17:49
-
More likely than not, your implementation stores floating points in the IEEE-754 format. In that format `216.612` does not have an exact representation, so the moment you store that number in a variable it's no longer *exactly* `216.612`, so no *exact* conversion can revert it to `216.612`. It becomes possible once you allow for tolerances, but in that case you have to decide in advance what precision you require. – dxiv Dec 01 '20 at 17:51
-
Maybe this: https://stackoverflow.com/questions/16839658/printf-width-specifier-to-maintain-precision-of-floating-point-value could answer your question. – KamilCuk Dec 01 '20 at 17:53
-
Thanks all a lot, I searched but I didn't see the other question – Alireza Mirzaei Dec 01 '20 at 18:05