Csharp/C Sharp/Development Class/Round — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 11:43, 26 мая 2010
Round down a number
using System;
using System.Data;
using System.Text.RegularExpressions;
class Class1{
static void Main(string[] args){
Console.WriteLine(RoundDown(.4));
Console.WriteLine(RoundDown(.5));
Console.WriteLine(RoundDown(.6));
Console.WriteLine(RoundDown(1.4));
Console.WriteLine(RoundDown(1.5));
Console.WriteLine(RoundDown(1.6));
Console.WriteLine(RoundDown(2.4));
Console.WriteLine(RoundDown(2.5));
Console.WriteLine(RoundDown(2.6));
Console.WriteLine(RoundDown(3.4));
Console.WriteLine(RoundDown(3.5));
Console.WriteLine(RoundDown(3.6));
}
public static double RoundDown(double valueToRound)
{
double floorValue = Math.Floor(valueToRound);
if ((valueToRound - floorValue) > .5)
{
return (floorValue + 1);
}
else
{
return (floorValue);
}
}
}
Rounding a Floating Point Value
using System;
using System.Data;
using System.Text.RegularExpressions;
class Class1{
static void Main(string[] args){
int X = (int)Math.Round(2.5555);
Console.WriteLine(X);
Console.WriteLine(Math.Round(2.5555, 2));
Console.WriteLine(Math.Round(2.444444,3));
Console.WriteLine(Math.Round(2.666666666666666666666666666660001));
Console.WriteLine(Math.Round(2.4444444444444444444444440001));
Console.WriteLine(Math.Round(.5));
Console.WriteLine(Math.Round(1.5));
Console.WriteLine(Math.Round(2.5));
Console.WriteLine(Math.Round(3.5));
Console.WriteLine();
Console.WriteLine(Math.Floor(.5));
Console.WriteLine(Math.Floor(1.5));
Console.WriteLine(Math.Floor(2.5));
Console.WriteLine(Math.Floor(3.5));
Console.WriteLine();
Console.WriteLine(Math.Ceiling(.5));
Console.WriteLine(Math.Ceiling(1.5));
Console.WriteLine(Math.Ceiling(2.5));
Console.WriteLine(Math.Ceiling(3.5));
Console.WriteLine();
}
}
Round up a number
using System;
using System.Data;
using System.Text.RegularExpressions;
class Class1{
static void Main(string[] args){
Console.WriteLine(RoundUp(.4));
Console.WriteLine(RoundUp(.5));
Console.WriteLine(RoundUp(.6));
Console.WriteLine(RoundUp(1.4));
Console.WriteLine(RoundUp(1.5));
Console.WriteLine(RoundUp(1.6));
Console.WriteLine(RoundUp(2.4));
Console.WriteLine(RoundUp(2.5));
Console.WriteLine(RoundUp(2.6));
Console.WriteLine(RoundUp(3.4));
Console.WriteLine(RoundUp(3.5));
Console.WriteLine(RoundUp(3.6));
}
public static double RoundUp(double valueToRound)
{
return (Math.Floor(valueToRound + 0.5));
}
}