In C#, I\'m trying to get the last two decimal places of a double with NO rounding. I\'ve tried everything from Math.Floor
to Math.Truncate
and nothin
Well, mathematically it's simple:
var f = 1.1234;
f = Math.Truncate(f * 100) / 100; // f == 1.12
Move the decimal two places to the right, cast to an int to truncate, shift it back to the left two places. There may be ways in the framework to do it too, but I can't look right now. You could generalize it:
double Truncate(double value, int places)
{
// not sure if you care to handle negative numbers...
var f = Math.Pow( 10, places );
return Math.Truncate( value * f ) / f;
}