CByte Function
Returns an expression that has been converted to a Variant of subtype Byte.
Syntax
CByte(expression)
Remarks
The expression argument is any valid expression.
Use the CByte
function to provide internationally aware conversions
from any other data type to a Byte subtype. For example, different decimal
separators are properly recognized depending on the locale setting of your system,
as are different thousand separators.
If expression lies outside the acceptable range for the byte subtype, an error
occurs. The following example uses the CByte
function to convert an
expression to a byte:
The following example illustrates the use of the CByte
function:
Example 1:
Dim MyDouble, MyByte
MyDouble = 125.5678 ' MyDouble is a Double.
MyByte = CByte(MyDouble)
'Output: 126
Example 2:
Dim MyInt
MyInt = 100 ' MyInt is an Integer.
MyByte = CByte(MyInt)
'Output: 100
Example 3:
Dim MyString
MyString = "50" ' MyString is a String.
MyByte = CByte(MyString)
'Output: 50
Example 4:
Dim MyDouble, MyByte
MyDouble = 125.4 ' MyDouble is a Double.
MyByte = CByte(MyDouble)
'Output: 125
Example 5:
Dim MyDouble, MyByte
MyDouble = 125.5 ' MyDouble is a Double.
MyByte = CByte(MyDouble) ' 125.5 round to 126 (nearest even number)
'Output: 126
Example 6:
Dim MyDouble, MyByte
MyDouble = 124.5 ' MyDouble is a Double.
MyByte = CByte(MyDouble) ' 124.5 round to 124 (nearest even number)
'Output: 124
Note:
CByte
differs from the Fix and Int functions, which truncate,
rather than round, the fractional part of a number. When the fractional part is
exactly 0.5, the CByte
function always rounds it to the nearest
even number. For example, 0.5 rounds to 0, and 1.5 rounds to 2.