Announcement: All noncommercial projects registered to use Earth Engine beforeApril 15, 2025 mustverify noncommercial eligibility to maintain access. If you have not verified by September 26, 2025, your access may be on hold.

ee.Number.byte

  • Thebyte() method casts a number to an unsigned 8-bit integer, resulting in a value between 0 and 255.

  • Casting a floating-point number to byte will result in the loss of decimal precision.

  • Numbers outside the byte range of 0 to 255 when cast to byte become either the maximum value (255) or the minimum value (0) of the byte range.

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

UsageReturns
Number.byte()Number
ArgumentTypeDetails
this:inputNumberThe input value.

Examples

Code Editor (JavaScript)

// Cast a number to unsigned 8-bit integer: [0, 255].varnumber=ee.Number(100);print('Number:',number);varbyteNumber=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.varfloat=ee.Number(1.7);print('Floating point value:',float);varfloatToByte=float.byte();print('Floating point value cast to byte:',floatToByte);// A number greater than byte range max cast to byte becomes byte range max.varBYTE_MAX=255;varoutOfRangeHi=ee.Number(BYTE_MAX+12345);print('Greater than byte max:',outOfRangeHi);varoutOfRangeHiToByte=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.varBYTE_MIN=0;varoutOfRangeLo=ee.Number(BYTE_MIN-12345);print('Less than byte min:',outOfRangeLo);varoutOfRangeLoToByte=outOfRangeLo.byte();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 usinggeemap for interactive development.

importeeimportgeemap.coreasgeemap

Colab (Python)

# Cast a number to unsigned 8-bit integer: [0, 255].number=ee.Number(100)display('Number:',number)byte_number=number.byte()display('Number cast to byte:',byte_number)"""Casting numbers to byte that are outside of its range and precision canmodify 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)display('Floating point value:',float_number)float_to_byte=float_number.byte()display('Floating point value cast to byte:',float_to_byte)# A number greater than byte range max cast to byte becomes byte range max.BYTE_MAX=255out_of_range_hi=ee.Number(BYTE_MAX+12345)display('Greater than byte max:',out_of_range_hi)out_of_range_hi_to_byte=out_of_range_hi.byte()display('Greater than byte max cast to byte becomes byte max:',out_of_range_hi_to_byte)# A number greater than byte range min cast to byte becomes byte range min.BYTE_MIN=0out_of_range_lo=ee.Number(BYTE_MIN-12345)display('Less than byte min:',out_of_range_lo)out_of_range_lo_to_byte=out_of_range_lo.byte()display('Less than byte min cast to byte becomes byte min:',out_of_range_lo_to_byte)

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2023-10-06 UTC.