ee.Number.format
Convert a number to a string using printf-style formatting.
Usage | Returns |
---|
Number.format(pattern) | String |
Argument | Type | Details |
---|
this: number | Number | The number to convert to a string. |
pattern | String, default: "%s" | A printf-style format string. For example, '%.2f' produces numbers formatted like '3.14', and '%05d' produces numbers formatted like '00042'. The format string must satisfy the following criteria:
- Zero or more prefix characters.
- Exactly one '%'.
- Zero or more modifier characters in the set [#-+ 0,(.\d].
- Exactly one conversion character in the set [sdoxXeEfgGaA].
- Zero or more suffix characters.
For more about format strings, see https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Formatter.html |
Examples
print('Zero-fill to length of 3',
ee.Number(1).format('%03d')); // 001
print('Include 1 decimal place in 1.2347',
ee.Number(1.23476).format('%.1f')); // 1.2
print('Include 3 decimal places in 1.2347',
ee.Number(1.23476).format('%.3f')); // 1.235 (rounds up)
print('Scientific notation with 3 decimal places shown',
ee.Number(123476).format('%.3e')); // 1.235e+05 (rounds up)
print('Integer with 2 decimal places of precision',
ee.Number(123476).format('%.2f')); // 123476.00
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
print('Zero-fill to length of 3:',
ee.Number(1).format('%03d').getInfo()) # 001
print('Include 1 decimal place in 1.2347:',
ee.Number(1.23476).format('%.1f').getInfo()) # 1.2
print('Include 3 decimal places in 1.2347:',
ee.Number(1.23476).format('%.3f').getInfo()) # 1.235 (rounds up)
print('Scientific notation with 3 decimal places shown:',
ee.Number(123476).format('%.3e').getInfo()) # 1.235e+05 (rounds up)
print('Integer with 2 decimal places of precision:',
ee.Number(123476).format('%.2f').getInfo()) # 123476.00
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 2024-09-19 UTC.
[null,null,["Last updated 2024-09-19 UTC."],[[["`Number.format()` converts Earth Engine Numbers to strings using printf-style formatting."],["The `pattern` argument accepts a format string, like '%.2f' for 2 decimal places or '%03d' for zero-padding to 3 digits."],["This function allows customization of numeric output for display or further processing within Earth Engine scripts."],["Refer to the Java Formatter documentation for details on available format string options and syntax."]]],[]]