I'm looking for some algorithm to extract fraction part of an f64/f32 provided, as an integer.
It should work sth like this:
get_frac(2.32_f64); // 32 get_frac(0.023_f64); // 23 get_frac(0.00402_f64); // 402
The easiest way I found is to
./parse slice, but it feels off to have to parse/unwrap when the input is already a float.
I looked into what the
Display do and they use grisu3 which seems to be a pretty nice algorithm, but also quite sophisticated and aimed at building a string out of a float, while I only need an integer which I'd hope is simpler.
I imagine that there would be some way to get
f64::get_parts() which would return integer and fraction as an integer, but I can't seem to find it, and all algorithmic approaches with multiplications/divisions in a loop run into the rounding error where
0.023_f64 ends up being
0.22999999997 and then
229999997 instead of
Any idea or a lead to an algorithm that may work for this use case?
(the use case is https://unicode.org/reports/tr35/tr35-numbers.html#Plural_Operand_Meanings )