ee.Number.format

Chuyển đổi một số thành chuỗi bằng cách sử dụng định dạng theo kiểu printf.

Cách sử dụngGiá trị trả về
Number.format(pattern)Chuỗi
Đối sốLoạiThông tin chi tiết
this: numberSốSố cần chuyển đổi thành chuỗi.
patternChuỗi, mặc định: "%s"Một chuỗi định dạng theo kiểu printf. Ví dụ: "%.2f" tạo ra các số được định dạng như "3.14" và "%05d" tạo ra các số được định dạng như "00042". Chuỗi định dạng phải đáp ứng các tiêu chí sau:
  1. Không có hoặc có nhiều ký tự tiền tố.
  2. Chỉ có một "%".
  3. Không có hoặc có nhiều ký tự sửa đổi trong tập hợp [#-+ 0,(.\d].
  4. Chính xác một ký tự chuyển đổi trong tập hợp [sdoxXeEfgGaA].
  5. Không có hoặc có một số ký tự hậu tố.
Để biết thêm về chuỗi định dạng, hãy xem https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Formatter.html

Ví dụ

Trình soạn thảo mã (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

Thiết lập Python

Hãy xem trang Môi trường Python để biết thông tin về API Python và cách sử dụng geemap cho quá trình phát triển tương tác.

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