how to trancate

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#
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top