ee.Number.toByte
Casts the input value to an unsigned 8-bit integer.
Usage | Returns |
---|
Number.toByte() | Number |
Argument | Type | Details |
---|
this: input | Number | The input value. |
Examples
// Cast a number to unsigned 8-bit integer: [0, 255].
var number = ee.Number(100);
print('Number:', number);
var byteNumber = number.toByte();
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.toByte();
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.toByte();
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.toByte();
print('Less than byte min cast to byte becomes byte min:', outOfRangeLoToByte);
Python setup
See the
Python Environment page for information on the Python API and using
geemap
for interactive development.
import ee
import geemap.core as geemap
# Cast a number to unsigned 8-bit integer: [0, 255].
number = ee.Number(100)
print('Number:', number.getInfo())
byte_number = number.toByte()
print('Number cast to byte:', byte_number.getInfo())
"""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.
float_number = ee.Number(1.7)
print('Floating point value:', float_number.getInfo())
float_to_byte = float_number.toByte()
print('Floating point value cast to byte:', float_to_byte.getInfo())
# A number greater than byte range max cast to byte becomes byte range max.
BYTE_MAX = 255
out_of_range_hi = ee.Number(BYTE_MAX + 12345)
print('Greater than byte max:', out_of_range_hi.getInfo())
out_of_range_hi_to_byte = out_of_range_hi.toByte()
print('Greater than byte max cast to byte becomes byte max:',
out_of_range_hi_to_byte.getInfo())
# A number greater than byte range min cast to byte becomes byte range min.
BYTE_MIN = 0
out_of_range_lo = ee.Number(BYTE_MIN - 12345)
print('Less than byte min:', out_of_range_lo.getInfo())
out_of_range_lo_to_byte = out_of_range_lo.toByte()
print('Less than byte min cast to byte becomes byte min:',
out_of_range_lo_to_byte.getInfo())
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2023-10-06 UTC.
[null,null,["Last updated 2023-10-06 UTC."],[[["`Number.toByte()` casts a given Earth Engine `Number` to an unsigned 8-bit integer, representing values within the range of 0 to 255."],["When casting floating-point numbers, the decimal precision is lost, resulting in the nearest integer value within the byte range."],["Input values exceeding the maximum byte value (255) are converted to 255, while values below the minimum (0) are converted to 0."],["This function is useful for ensuring data is stored in a compact 8-bit format, which can be beneficial for memory management and data transfer efficiency."]]],["The `toByte()` method casts a number to an unsigned 8-bit integer, ranging from 0 to 255. Floating-point numbers lose their decimal precision. Numbers exceeding the maximum byte range (255) are set to 255, while numbers below the minimum (0) are set to 0. This method takes an input number and returns it as an 8-bit number.\n"]]