how to trancate

  • Thread starter Thread starter uto
  • Start date Start date
U

uto

i'd like to trancate like this.

1.555 => 1
2.6655 = > 2
0.901923 = > 0

and

1.56 => 1.5
1.34 => 1.3
1.99 => 1.9

how can i do this in c#
 
uto,

It seems like you want a floor function. There is nothing like this in
..NET which can do this for you, but it is easy enough to program.

public static double Floor(double value)
{
// Just return the overload.
return Floor(value, 0);
}

public static double Floor(double value, int digits)
{
// Check here to see if digits is negative.

// Get the amount to subtract from value. Generally, if you are
rounding
// to 0 places, then you would subtract .5, and round normally. If to
// 1 place, then subtract .05, etc, etc.
int powerOfTen = Math.Pow(10, digits);

// Now subtract, and round.
return Math.Round(value - (.5 * powerOfTen), digits);
}

Hope this helps.
 
Back
Top