Announcement: All noncommercial projects registered to use Earth Engine before April 15, 2025 must verify noncommercial eligibility to maintain Earth Engine access.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2023-10-06 UTC."],[[["\u003cp\u003e\u003ccode\u003eNumber.toByte()\u003c/code\u003e casts a given Earth Engine \u003ccode\u003eNumber\u003c/code\u003e to an unsigned 8-bit integer, representing values within the range of 0 to 255.\u003c/p\u003e\n"],["\u003cp\u003eWhen casting floating-point numbers, the decimal precision is lost, resulting in the nearest integer value within the byte range.\u003c/p\u003e\n"],["\u003cp\u003eInput values exceeding the maximum byte value (255) are converted to 255, while values below the minimum (0) are converted to 0.\u003c/p\u003e\n"],["\u003cp\u003eThis 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.\u003c/p\u003e\n"]]],["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"],null,["# ee.Number.toByte\n\nCasts the input value to an unsigned 8-bit integer.\n\n\u003cbr /\u003e\n\n| Usage | Returns |\n|-------------------|---------|\n| Number.toByte`()` | Number |\n\n| Argument | Type | Details |\n|---------------|--------|------------------|\n| this: `input` | Number | The input value. |\n\nExamples\n--------\n\n### Code Editor (JavaScript)\n\n```javascript\n// Cast a number to unsigned 8-bit integer: [0, 255].\nvar number = ee.Number(100);\nprint('Number:', number);\n\nvar byteNumber = number.toByte();\nprint('Number cast to byte:', byteNumber);\n\n\n/**\n * Casting numbers to byte that are outside of its range and precision can\n * modify the resulting value, note the behavior of the following scenarios.\n */\n\n// A floating point number cast to byte loses decimal precision.\nvar float = ee.Number(1.7);\nprint('Floating point value:', float);\n\nvar floatToByte = float.toByte();\nprint('Floating point value cast to byte:', floatToByte);\n\n// A number greater than byte range max cast to byte becomes byte range max.\nvar BYTE_MAX = 255;\nvar outOfRangeHi = ee.Number(BYTE_MAX + 12345);\nprint('Greater than byte max:', outOfRangeHi);\n\nvar outOfRangeHiToByte = outOfRangeHi.toByte();\nprint('Greater than byte max cast to byte becomes byte max:', outOfRangeHiToByte);\n\n// A number greater than byte range min cast to byte becomes byte range min.\nvar BYTE_MIN = 0;\nvar outOfRangeLo = ee.Number(BYTE_MIN - 12345);\nprint('Less than byte min:', outOfRangeLo);\n\nvar outOfRangeLoToByte = outOfRangeLo.toByte();\nprint('Less than byte min cast to byte becomes byte min:', outOfRangeLoToByte);\n```\nPython setup\n\nSee the [Python Environment](/earth-engine/guides/python_install) page for information on the Python API and using\n`geemap` for interactive development. \n\n```python\nimport ee\nimport geemap.core as geemap\n```\n\n### Colab (Python)\n\n```python\n# Cast a number to unsigned 8-bit integer: [0, 255].\nnumber = ee.Number(100)\nprint('Number:', number.getInfo())\n\nbyte_number = number.toByte()\nprint('Number cast to byte:', byte_number.getInfo())\n\n\n\"\"\"Casting numbers to byte that are outside of its range and precision can\nmodify the resulting value, note the behavior of the following scenarios.\n\"\"\"\n\n# A floating point number cast to byte loses decimal precision.\nfloat_number = ee.Number(1.7)\nprint('Floating point value:', float_number.getInfo())\n\nfloat_to_byte = float_number.toByte()\nprint('Floating point value cast to byte:', float_to_byte.getInfo())\n\n# A number greater than byte range max cast to byte becomes byte range max.\nBYTE_MAX = 255\nout_of_range_hi = ee.Number(BYTE_MAX + 12345)\nprint('Greater than byte max:', out_of_range_hi.getInfo())\n\nout_of_range_hi_to_byte = out_of_range_hi.toByte()\nprint('Greater than byte max cast to byte becomes byte max:',\n out_of_range_hi_to_byte.getInfo())\n\n# A number greater than byte range min cast to byte becomes byte range min.\nBYTE_MIN = 0\nout_of_range_lo = ee.Number(BYTE_MIN - 12345)\nprint('Less than byte min:', out_of_range_lo.getInfo())\n\nout_of_range_lo_to_byte = out_of_range_lo.toByte()\nprint('Less than byte min cast to byte becomes byte min:',\n out_of_range_lo_to_byte.getInfo())\n```"]]