Assuming you want to return the positive part even for negative values, I would go with
(int)round(fabs(value - trunc(value)) * 1e4)
which should give the expected result of 1234 .
If you donβt round and just crop the value
(int)(fabs(value - trunc(value)) * 1e4)
(which essentially matches your source code), you get an unexpected result of 1233 as 1.1234 - 1.0 = 0.12339999999999995 in double precision.
Without using round() you will also get the expected result if you change the order of operations to
(int)(fabs(value * 1e4 - trunc(value) * 1e4))
If the integral part of value is large enough, floating point inaccuracies will, of course, be removed again.
You can also use modf() instead of trunc() , as David suggests, which is probably the best approach regarding floating point precision:
double dummy; (int)round(fabs(modf(value, &dummy)) * 1e4)
source share