C#でDecimal.Round()メソッド
C#でDecimal.Round()メソッドは、小数点以下の最も近い整数に値または指定された数を丸めるために使用される。
構文
以下は構文です-
public static decimal Round (decimal d); public static decimal Round (decimal d, int decimals); public static decimal Round (decimal d, MidpointRounding mode); public static decimal Round (decimal d, int decimals, MidpointRounding mode);
例
私たちは今Decimal.Round()メソッドを実装するための例を見てみましょう -
システムを使用して、using System;
public class Demo {
public static void Main(){
Decimal val1 = 9.00m;
Decimal val2 = 15.29m;
Decimal val3 = 394949845.14245M;
Console.WriteLine("Decimal 1 = "+val1);
Console.WriteLine("Decimal 2 = "+val2);
Console.WriteLine("Decimal 2 = "+val2);
Console.WriteLine("Remainder = "+(Decimal.Remainder(val1,val2)));
Console.WriteLine("Value 1 (Rounded) = "+(Decimal.Round(val1)));
Console.WriteLine("Value 2 (Rounded) = "+(Decimal.Round(val2)));
Console.WriteLine("Value 3 (Rounded) = "+(Decimal.Round(val3)));
}
} 出力
これにより、次の出力が生成されます-
Decimal 1 = 9.00 Decimal 2 = 15.29 Decimal 2 = 15.29 Remainder = 9.00 Value 1 (Rounded) = 9 Value 2 (Rounded) = 15 Value 3 (Rounded) = 394949845
例
私たちは今Decimal.Round()メソッドを実装する別の例を見てみましょう -
システムを使用して、using System;
public class Demo {
public static void Main(){
Decimal val1 = 6.59m;
Decimal val2 = 30.12m;
Decimal val3 = 6946649845.25245M;
Console.WriteLine("Decimal 1 = "+val1);
Console.WriteLine("Decimal 2 = "+val2);
Console.WriteLine("Decimal 2 = "+val2);
Console.WriteLine("Remainder = "+(Decimal.Remainder(val1,val2)));
Console.WriteLine("Value 1 (Rounded) = "+(Decimal.Round(val1,1)));
Console.WriteLine("Value 2 (Rounded) = "+(Decimal.Round(val2,1)));
Console.WriteLine("Value 3 (Rounded) = "+(Decimal.Round(val3,2)));
}
} 出力
これにより、次の出力が生成されます-
Decimal 1 = 6.59 Decimal 2 = 30.12 Decimal 2 = 30.12 Remainder = 6.59 Value 1 (Rounded) = 6.6 Value 2 (Rounded) = 30.1 Value 3 (Rounded) = 6946649845.25
-
C#のDecimal.ToOACurrency()メソッド
C#のDecimal.ToOACurrency()メソッドは、指定されたDecimal値を、64ビットの符号付き整数に含まれる同等のOLEオートメーション通貨値に変換するために使用されます。 構文 以下は構文です- public static long ToOACurrency (decimal val); 上記のValは、変換する10進数です。 例 Decimal.ToOACurrency()メソッドを実装する例を見てみましょう- using System; public class Demo { public static void Main(){ &nbs
-
C#のDecimal.Floor()メソッド
C#のDecimal.Floor()メソッドは、指定された10進数を負の無限大に最も近い整数に丸めます。 構文 以下は構文です- public static decimal Floor (decimal val); 上記では、Valは丸める値です。 例 Decimal.Floor()メソッドを実装する例を見てみましょう- using System; public class Demo { public static void Main(){ Decimal val1 = 25.25m; &nb