Decimal.ToSByte Method
Microsoft Silverlight will reach end of support after October 2021. Learn more.
Converts the value of the specified Decimal to the equivalent 8-bit signed integer.
This API is not CLS-compliant. The CLS-compliant alternative is ToInt16(Decimal).
Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Syntax
'Declaration
<CLSCompliantAttribute(False)> _
Public Shared Function ToSByte ( _
value As Decimal _
) As SByte
[CLSCompliantAttribute(false)]
public static sbyte ToSByte(
decimal value
)
Parameters
- value
Type: System.Decimal
A Decimal value.
Return Value
Type: System.SByte
An 8-bit signed integer equivalent to value.
Exceptions
Exception | Condition |
---|---|
OverflowException | value is less than SByte.MinValue or greater than SByte.MaxValue. |
Examples
The following code example converts Decimal numbers to SByte values using the ToSByte method.
' Example of the Decimal.ToSByte and Decimal.ToByte methods.
Module Example
Dim formatter As String = "{0,16}{1,19}{2,19}"
' Get the exception type name; remove the namespace prefix.
Function GetExceptionType(ByVal ex As Exception) As String
Dim exceptionType As String = ex.GetType().ToString()
Return exceptionType.Substring( _
exceptionType.LastIndexOf("."c) + 1)
End Function
' Convert the Decimal argument; catch exceptions that are thrown.
Sub DecimalToS_Byte(ByVal outputBlock As System.Windows.Controls.TextBlock, ByVal argument As Decimal)
Dim SByteValue As Object
Dim ByteValue As Object
' Convert the argument to an SByte value.
Try
SByteValue = Decimal.ToSByte(argument)
Catch ex As Exception
SByteValue = GetExceptionType(ex)
End Try
' Convert the argument to a Byte value.
Try
ByteValue = Decimal.ToByte(argument)
Catch ex As Exception
ByteValue = GetExceptionType(ex)
End Try
outputBlock.Text &= String.Format(formatter, argument, _
SByteValue, ByteValue) & vbCrLf
End Sub
Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
outputBlock.Text &= String.Format("This example of the " & vbCrLf & _
" Decimal.ToSByte( Decimal ) and " & vbCrLf & _
" Decimal.ToByte( Decimal ) " & vbCrLf & "methods " & _
"generates the following output. It " & vbCrLf & _
"displays several converted Decimal values." & vbCrLf) & vbCrLf
outputBlock.Text &= String.Format(formatter, "Decimal argument", _
"SByte/exception", "Byte/exception") & vbCrLf
outputBlock.Text &= String.Format(formatter, "----------------", _
"---------------", "--------------") & vbCrLf
' Convert Decimal values and display the results.
DecimalToS_Byte(outputBlock, 78D)
DecimalToS_Byte(outputBlock, New Decimal(78000, 0, 0, False, 3))
DecimalToS_Byte(outputBlock, 78.999D)
DecimalToS_Byte(outputBlock, 255.999D)
DecimalToS_Byte(outputBlock, 256D)
DecimalToS_Byte(outputBlock, 127.999D)
DecimalToS_Byte(outputBlock, 128D)
DecimalToS_Byte(outputBlock, -0.999D)
DecimalToS_Byte(outputBlock, -1D)
DecimalToS_Byte(outputBlock, -128.999D)
DecimalToS_Byte(outputBlock, -129D)
End Sub
End Module
' This example of the
' Decimal.ToSByte( Decimal ) and
' Decimal.ToByte( Decimal )
' methods generates the following output. It
' displays several converted Decimal values.
'
' Decimal argument SByte/exception Byte/exception
' ---------------- --------------- --------------
' 78 78 78
' 78.000 78 78
' 78.999 78 78
' 255.999 OverflowException 255
' 256 OverflowException OverflowException
' 127.999 127 127
' 128 OverflowException 128
' -0.999 0 0
' -1 -1 OverflowException
' -128.999 -128 OverflowException
' -129 OverflowException OverflowException
// Example of the decimal.ToSByte and decimal.ToByte methods.
using System;
class Example
{
const string formatter = "{0,16}{1,19}{2,19}";
// Get the exception type name; remove the namespace prefix.
public static string GetExceptionType(Exception ex)
{
string exceptionType = ex.GetType().ToString();
return exceptionType.Substring(
exceptionType.LastIndexOf('.') + 1);
}
// Convert the decimal argument; catch exceptions that are thrown.
public static void DecimalToS_Byte(System.Windows.Controls.TextBlock outputBlock, decimal argument)
{
object SByteValue;
object ByteValue;
// Convert the argument to an sbyte value.
try
{
SByteValue = decimal.ToSByte(argument);
}
catch (Exception ex)
{
SByteValue = GetExceptionType(ex);
}
// Convert the argument to a byte value.
try
{
ByteValue = decimal.ToByte(argument);
}
catch (Exception ex)
{
ByteValue = GetExceptionType(ex);
}
outputBlock.Text += String.Format(formatter, argument,
SByteValue, ByteValue) + "\n";
}
public static void Demo(System.Windows.Controls.TextBlock outputBlock)
{
outputBlock.Text += String.Format("This example of the \n" +
" decimal.ToSByte( decimal ) and \n" +
" decimal.ToByte( decimal ) \nmethods " +
"generates the following output. It \ndisplays " +
"several converted decimal values.\n") + "\n";
outputBlock.Text += String.Format(formatter, "decimal argument",
"sbyte/exception", "byte/exception") + "\n";
outputBlock.Text += String.Format(formatter, "----------------",
"---------------", "--------------") + "\n";
// Convert decimal values and display the results.
DecimalToS_Byte(outputBlock, 78M);
DecimalToS_Byte(outputBlock, new decimal(78000, 0, 0, false, 3));
DecimalToS_Byte(outputBlock, 78.999M);
DecimalToS_Byte(outputBlock, 255.999M);
DecimalToS_Byte(outputBlock, 256M);
DecimalToS_Byte(outputBlock, 127.999M);
DecimalToS_Byte(outputBlock, 128M);
DecimalToS_Byte(outputBlock, -0.999M);
DecimalToS_Byte(outputBlock, -1M);
DecimalToS_Byte(outputBlock, -128.999M);
DecimalToS_Byte(outputBlock, -129M);
}
}
/*
This example of the
decimal.ToSByte( decimal ) and
decimal.ToByte( decimal )
methods generates the following output. It
displays several converted decimal values.
decimal argument sbyte/exception byte/exception
---------------- --------------- --------------
78 78 78
78.000 78 78
78.999 78 78
255.999 OverflowException 255
256 OverflowException OverflowException
127.999 127 127
128 OverflowException 128
-0.999 0 0
-1 -1 OverflowException
-128.999 -128 OverflowException
-129 OverflowException OverflowException
*/
Version Information
Silverlight
Supported in: 5, 4, 3
Silverlight for Windows Phone
Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0
XNA Framework
Supported in: Xbox 360, Windows Phone OS 7.0
Platforms
For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.