ee.Number.byte

Casts the input value to an unsigned 8-bit integer.

UsageReturns
Number.byte()Number
ArgumentTypeDetails
this: inputNumber

The input value.

Examples

Code Editor (JavaScript)

// Cast a number to unsigned 8-bit integer: [0, 255].
var number = ee.Number(100);
print('Number:', number);

var byteNumber = number.byte();
print('Number cast to byte:', byteNumber);


/**
 * Casting numbers to byte that are outside of its range and precision can
 * modify the resulting value, note the behavior of the following scenarios.
 */

// A floating point number cast to byte loses decimal precision.
var float = ee.Number(1.7);
print('Floating point value:', float);

var floatToByte = float.byte();
print('Floating point value cast to byte:', floatToByte);

// A number greater than byte range max cast to byte becomes byte range max.
var BYTE_MAX = 255;
var outOfRangeHi = ee.Number(BYTE_MAX + 12345);
print('Greater than byte max:', outOfRangeHi);

var outOfRangeHiToByte = outOfRangeHi.byte();
print('Greater than byte max cast to byte becomes byte max:', outOfRangeHiToByte);

// A number greater than byte range min cast to byte becomes byte range min.
var BYTE_MIN = 0;
var outOfRangeLo = ee.Number(BYTE_MIN - 12345);
print('Less than byte min:', outOfRangeLo);

var outOfRangeLoToByte = outOfRangeLo.byte();
print('Less than byte min cast to byte becomes byte min:', outOfRangeLoToByte);