ee.Number.format

使用 printf 樣式格式將數字轉換為字串。

用量傳回
Number.format(pattern)字串
引數類型詳細資料
這個:number數字要轉換為字串的數字。
pattern字串,預設值為「%s」printf 樣式的格式字串。舉例來說,'%.2f' 會產生格式為 '3.14' 的數字,而 '%05d' 會產生格式為 '00042' 的數字。格式字串必須符合下列條件:
  1. 零或多個前置字元。
  2. 只有一個「%」。
  3. 集合 [#-+ 0,(.\d] 中的零或多個修飾符字元。
  4. 集合 [sdoxXeEfgGaA] 中只能有一個轉換字元。
  5. 零或多個後置字元。
如要進一步瞭解格式字串,請參閱 https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Formatter.html

範例

程式碼編輯器 (JavaScript)

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 設定

請參閱 Python 環境頁面,瞭解 Python API 和如何使用 geemap 進行互動式開發。

import ee
import geemap.core as geemap

Colab (Python)

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