See this for understanding the limitations of using decimal or float Types to express numeric values: [
^].
private const int SIGN_MASK = ~Int32.MinValue;
public int GetDecimalRemainder(float fvalue)
{
double dplaces;
try
{
decimal dvalue = Convert.ToDecimal(fvalue);
dplaces = (double) ((Decimal.GetBits(dvalue)[3] & SIGN_MASK) >> 16);
return (int) ((dvalue - Math.Truncate(dvalue)) * (int) Math.Pow(10d, dplaces));
}
catch (Exception ex)
{
throw new TypeInitializationException(@"{fvalue} cannot be converted", ex);
}
}
Notes:
0 note that conversion to decimal may round the value, leading to loss og precision in the mantissa.
1 this will return a negative number if the input float is negative
2 use of 'GetBits to find the number of powers of ten in a float mantissa iis based on: [
^]