I'm having trouble formulating a good way to extract the decimal part of a decimal
variable, so far the existing implementation on the system is this:
public static bool getCasasDecimais(decimal val, out int result)
{
string[] split = val.ToString().Split('.');
return int.TryParse(split[split.Length-1], out result);
}
But it has a problem, you have to adapt the code depending on the culture in which the program is running because of the number division by the decimal point.
I'm trying to find some other implementation to remove that part of the number, preferably something that does not handle a string
to perform such an operation. The only way I came to mind was by using Truncate
:
decimal number = 12.5523;
var x = number - Math.Truncate(number);
But for some reason, this implementation does not seem very robust, I do not really know why, but I'd like to see other possible implementations before deciding which one I'll use.
After a comment from the user dcastro in the question, I decided to retry the implementation specified above and it does not return the expected value, since I want to have the decimal digits in an integer, not just the decimal part.
Exemplo: 45.545
Resultado esperado: 545
Resultado proveniente da implementação com Math.Truncate: 0.545
The question remains, is there a way to get this value without manipulating the number as a string?