C# Convert ToDecimal(Int64)
Description
Convert ToDecimal(Int64)
converts the value of the specified
64-bit signed integer to an equivalent decimal number.
Syntax
Convert.ToDecimal(Int64)
has the following syntax.
public static decimal ToDecimal(
long value
)
Parameters
Convert.ToDecimal(Int64)
has the following parameters.
value
- The 64-bit signed integer to convert.
Returns
Convert.ToDecimal(Int64)
method returns A decimal number that is equivalent to value.
Example
The following example converts an Int64 value to a Decimal value.
/*from w ww . j av a2s .c o m*/
using System;
public class MainClass
{
public static void Main(String[] argv)
{
long longVal = 123L;
decimal decimalVal;
// Long to decimal conversion cannot overflow.
decimalVal = System.Convert.ToDecimal(longVal);
System.Console.WriteLine("{0} as a decimal is {1}",
longVal, decimalVal);
// Decimal to long conversion can overflow.
try
{
longVal = System.Convert.ToInt64(decimalVal);
System.Console.WriteLine("{0} as a long is {1}",
decimalVal, longVal);
}
catch (System.OverflowException)
{
System.Console.WriteLine(
"Overflow in decimal-to-long conversion.");
}
}
}
The code above generates the following result.