Hướng dẫn php numberformatter - php numberformatter

Lớp NumberFormatter

(Php 5> = 5.3.0, Php 7, Php 8, Pecl Intl> = 1.0.0)

Show

简介

Các chương trình lưu trữ và hoạt động trên các số bằng cách sử dụng đại diện nhị phân độc lập với địa phương. Khi hiển thị hoặc in một số, nó được chuyển đổi thành chuỗi dành riêng cho địa phương. Ví dụ, số 12345.67 là "12,345,67" ở Mỹ, "12 345,67" ở Pháp và "12.345,67" ở Đức.

Bằng cách gọi các phương thức được cung cấp bởi lớp NumberFormatter, bạn có thể định dạng số, tiền tệ và tỷ lệ phần trăm theo ngôn ngữ được chỉ định hoặc mặc định. NumberFormatter là nhạy cảm với locale, vì vậy bạn cần tạo một numberFormatter mới cho mỗi địa phương. Phương thức NumberFormatter định dạng số loại nguyên thủy, chẳng hạn như gấp đôi và đầu ra số dưới dạng chuỗi cụ thể địa phương.

Đối với các loại tiền tệ, bạn có thể sử dụng loại định dạng tiền tệ để tạo một định dạng trả về một chuỗi với số được định dạng và dấu hiệu tiền tệ thích hợp. Tất nhiên, lớp NumberFormatter không biết về tỷ giá hối đoái, vì vậy, đầu ra số là giống nhau bất kể loại tiền được chỉ định. Điều này có nghĩa là cùng một số có các giá trị tiền tệ khác nhau tùy thuộc vào địa phương tiền tệ. Nếu số là 9988776,65, kết quả sẽ là:

  • 9 988 776,65 € ở Pháp
  • 9,988,776,65 € ở Đức
  • $ 9,988,776,65 tại Hoa Kỳ

Để định dạng tỷ lệ phần trăm, hãy tạo một định dạng cụ thể địa phương với loại định dạng phần trăm. Với định dạng này, một phần thập phân như 0,75 được hiển thị là 75%.

Đối với định dạng phức tạp hơn, như các số đánh vần, các định dạng số dựa trên quy tắc được sử dụng.

classNumberFormatter { NumberFormatter {

public__construct (Chuỗi $locale, int $style,? chuỗi $pattern = null) __construct(string $locale, int $style, ?string $pattern = null)

publicstaticCreate (Chuỗi $locale, int $style,? chuỗi $pattern = null):? numberFormatter static create(string $locale, int $style, ?string $pattern = null): ?NumberFormatter

publicFormatCurrency (float $amount, String $currency): Chuỗi | Sai formatCurrency(float $amount, string $currency): string|false

publicFormat (int | float $style0, int $style1 = numberFormatter :: type_default): chuỗi | false format(int|float $style0, int $style1 = NumberFormatter::TYPE_DEFAULT): string|false

publicGetAttribution (int $style2): int | float | false getAttribute(int $style2): int|float|false

publicGetLocale (int $style1 = $style4): Chuỗi | Sai getLocale(int $style1 = $style4): string|false

PublicGetSymbol (int $style5): Chuỗi | Sai getSymbol(int $style5): string|false

publicParsecurrency (Chuỗi $style6, chuỗi $style7, int $style8 = null): float | false parseCurrency(string $style6, string $style7, int $style8 = null): float|false

publicParse (Chuỗi $style6, int $style1 = numberFormatter :: type_double, int $style8 = null): int | float | false parse(string $style6, int $style1 = NumberFormatter::TYPE_DOUBLE, int $style8 = null): int|float|false

publicsetattribution (int $style2, int | float $pattern5): bool setAttribute(int $style2, int|float $pattern5): bool

PublicsetSyMbol (int $style5, String $pattern5): Bool setSymbol(int $style5, string $pattern5): bool

}

预定义

Các kiểu này được sử dụng bởi numfmt_create () để xác định loại định dạng.numfmt_create() to define the type of the formatter.

$pattern8 (int) định dạng thập phân được xác định bởi mẫu ____29 (int) định dạng thập phân ____ (int) định dạng tiền tệ ____31 (int) phần trăm định dạng phần trăm ____32 (int) Định dạng khoa học dựa trên quy tắc chính tả Định dạng dựa trên định dạng dựa trên quy tắc được xác định bởi định dạng tiền tệ mẫu ____37 (int) cho kế toán, ví dụ: null8 cho số tiền tiền tệ thay vì null9. Có sẵn kể từ Php 7.4.1 và ICU 53. $locale0 (int) Định dạng mặc định cho ALIAS địa phương (int) Decimal format defined by pattern $pattern9 (int) Decimal format null0 (int) Currency format null1 (int) Percent format null2 (int) Scientific format null3 (int) Spellout rule-based format null4 (int) Ordinal rule-based format null5 (int) Duration rule-based format null6 (int) Rule-based format defined by pattern null7 (int) Currency format for accounting, e.g., null8 for negative currency amount instead of null9. Available as of PHP 7.4.1 and ICU 53. $locale0 (int) Default format for the locale $locale1 (int) Alias for PATTERN_DECIMAL

Các hằng số này xác định cách các số được phân tích cú pháp hoặc định dạng. Chúng nên được sử dụng làm đối số cho numfmt_format () và numfmt_parse ().numfmt_format() and numfmt_parse().

$locale2 (int) xuất phát loại từ định dạng loại biến/định dạng/phân tích phân tích 32 bit định dạng/parse là số nguyên 64 bit ____ giá trị (int) Derive the type from variable type $locale3 (int) Format/parse as 32-bit integer $locale4 (int) Format/parse as 64-bit integer $locale5 (int) Format/parse as floating point value $locale6 (int) Format/parse as currency value

Thuộc tính định dạng số được sử dụng bởi numfmt_get_attribution () và numfmt_set_attribution ().numfmt_get_attribute() and numfmt_set_attribute().

$locale7 (int) chỉ phân tích các số nguyên .________ 48 (int) Sử dụng phân tách nhóm .________ 49 (int) Luôn hiển thị số thập phân .____ ____ 50 (int) Các chữ số số nguyên tối đa. int) Các chữ số phân số tối đa .________ 54 (int) Các chữ số phân số tối thiểu .________ 55 (int) Các chữ số phân số .________ 56 (int) Số nhân .________ 57 (int) Kích thước nhóm. int) chiều rộng mà đầu ra của định dạng () được đệm .________ 61 (int) vị trí mà tại đó sẽ diễn ra. Xem Hằng số vị trí PAD cho các giá trị đối số có thể. $pattern2 (int) Kích thước nhóm thứ cấp .________ 63 (int) Sử dụng các chữ số quan trọng .________ 64 (int) Các chữ số có ý nghĩa tối thiểu .________ 65 (int) Các chữ số đáng kể tối đa. (int) Parse integers only. $locale8 (int) Use grouping separator. $locale9 (int) Always show decimal point. $style0 (int) Maximum integer digits. $style1 (int) Minimum integer digits. $style2 (int) Integer digits. $style3 (int) Maximum fraction digits. $style4 (int) Minimum fraction digits. $style5 (int) Fraction digits. $style6 (int) Multiplier. $style7 (int) Grouping size. $style8 (int) Rounding Mode. $style9 (int) Rounding increment. $pattern0 (int) The width to which the output of format() is padded. $pattern1 (int) The position at which padding will take place. See pad position constants for possible argument values. $pattern2 (int) Secondary grouping size. $pattern3 (int) Use significant digits. $pattern4 (int) Minimum significant digits. $pattern5 (int) Maximum significant digits. $pattern6 (int) Lenient parse mode used by rule-based formats.

Định dạng số Thuộc tính văn bản được sử dụng bởi numfmt_get_text_attribute () và numfmt_set_text_attribution ().numfmt_get_text_attribute() and numfmt_set_text_attribute().

$pattern7 (int) Tiền tố dương .________ 68 (int) Hậu tố dương .________ 69 (int) Tiền tố âm .________ 70 (int) Hậu tố âm. .________ 73 (int) Bộ quy tắc mặc định. Điều này chỉ có sẵn với các định dạng dựa trên quy tắc. null4 (int) Các quy tắc công khai. Điều này chỉ có sẵn với các định dạng dựa trên quy tắc. Đây là một thuộc tính chỉ đọc. Các quy tắc công cộng được trả về dưới dạng một chuỗi duy nhất, với mỗi tên quy tắc được phân định bởi ';' (Semicolon). (int) Positive prefix. $pattern8 (int) Positive suffix. $pattern9 (int) Negative prefix. null0 (int) Negative suffix. null1 (int) The character used to pad to the format width. null2 (int) The ISO currency code. null3 (int) The default rule set. This is only available with rule-based formatters. null4 (int) The public rule sets. This is only available with rule-based formatters. This is a read-only attribute. The public rulesets are returned as a single string, with each ruleset name delimited by ';' (semicolon).

Các ký hiệu định dạng số được sử dụng bởi numfmt_get_symbol () và numfmt_set_symbol ().numfmt_get_symbol() and numfmt_set_symbol().

null5 (int) Bộ phân cách thập phân .________ 76 (int) Bộ phân cách nhóm .________ 77 (int) Bộ phân cách mẫu .________ 78 (int) Dấu phần trăm. $amount1 (int) dấu trừ .________ 82 (int) Dấu cộng cộng .________ 83 (int) Biểu tượng tiền tệ .________ 84 (int) Biểu tượng tiền tệ quốc tế. . (int) The decimal separator. null6 (int) The grouping separator. null7 (int) The pattern separator. null8 (int) The percent sign. null9 (int) Zero. $amount0 (int) Character representing a digit in the pattern. $amount1 (int) The minus sign. $amount2 (int) The plus sign. $amount3 (int) The currency symbol. $amount4 (int) The international currency symbol. $amount5 (int) The monetary separator. $amount6 (int) The exponential symbol. $amount7 (int) Per mill symbol. $amount8 (int) Escape padding character. $amount9 (int) Infinity symbol. $currency0 (int) Not-a-number symbol. $currency1 (int) Significant digit symbol. $currency2 (int) The monetary grouping separator.

Các giá trị chế độ làm tròn được sử dụng bởi numfmt_get_attribution () và numfmt_set_attribution () với thuộc tính $style8.numfmt_get_attribute() and numfmt_set_attribute() with $style8 attribute.

$currency4 (int) Chế độ làm tròn để làm tròn về phía vô cực dương .________ 95 (int) Chế độ làm tròn để làm tròn về không. Hàng xóm là bình đẳng, trong trường hợp đó làm tròn. $currency8 (int) Chế độ làm tròn để làm tròn về phía "người hàng xóm gần nhất" trừ khi cả hai hàng xóm đều tương đương, trong trường hợp đó, tròn về phía người hàng xóm. $currency9 (int) Chế độ làm tròn để làm tròn về phía "hàng xóm gần nhất" trừ khi cả hai hàng xóm đều đều tương đương, trong trường hợp đó làm tròn. $style00 (int) Chế độ làm tròn để làm tròn từ số không. (int) Rounding mode to round towards positive infinity. $currency5 (int) Rounding mode to round towards zero. $currency6 (int) Rounding mode to round towards negative infinity. $currency7 (int) Rounding mode to round towards "nearest neighbor" unless both neighbors are equidistant, in which case round down. $currency8 (int) Rounding mode to round towards the "nearest neighbor" unless both neighbors are equidistant, in which case, round towards the even neighbor. $currency9 (int) Rounding mode to round towards "nearest neighbor" unless both neighbors are equidistant, in which case round up. $style00 (int) Rounding mode to round away from zero.

Các giá trị vị trí PAD được sử dụng bởi numfmt_get_attribution () và numfmt_set_attribution () với thuộc tính $pattern1.numfmt_get_attribute() and numfmt_set_attribute() with $pattern1 attribute.

$style02 (int) Các ký tự pad được chèn sau tiền tố .________ 103 (int) Các ký tự pad được chèn sau hậu tố .________ 104 (int) Các ký tự pad được chèn trước tiền tố. (int) Pad characters inserted after the prefix. $style03 (int) Pad characters inserted after the suffix. $style04 (int) Pad characters inserted before the prefix. $style05 (int) Pad characters inserted before the suffix.

参见

  • »& Nbsp; Tài liệu định dạng ICU
  • »& Nbsp; định dạng số ICU
  • »& Nbsp; định dạng số thập phân ICU
  • »& Nbsp; Các định dạng số dựa trên quy tắc ICU

目录

  • NumberFormatter :: Tạo - Tạo một số định dạng số
  • NumberFormatter :: định dạng - định dạng giá trị tiền tệ
  • NumberFormatter :: Định dạng - Định dạng một số
  • NumberFormatter :: GetAttribution - Nhận thuộc tính
  • NumberFormatter :: GetErrorCode - Nhận mã lỗi cuối cùng của Formatter
  • NumberFormatter :: GeterRorMessage - Nhận thông báo lỗi cuối cùng của Formatter
  • NumberFormatter :: GetLocale - Nhận địa phương định dạng
  • NumberFormatter :: GetPottern - Nhận mẫu định dạng
  • NumberFormatter :: GetSyMbol - Nhận giá trị biểu tượng
  • NumberFormatter :: GetTextAttribution - Nhận thuộc tính văn bản
  • NumberFormatter :: Parsecurrence - phân tích số tiền tệ
  • NumberFormatter :: parse - phân tích một số
  • NumberFormatter :: SetAttribution - Đặt thuộc tính
  • NumberFormatter :: SetPottern - Đặt mẫu định dạng
  • NumberFormatter :: SetSymbol - Đặt giá trị biểu tượng
  • NumberFormatter :: SetTextAttribution - Đặt thuộc tính văn bản

Giorgio Dot Liscio tại email chấm nó ¶

11 năm trước

$style06

$style07

$style08

$style09

$style10

$style11

$style12

Stan tại Dragnev Dot ca ¶

1 năm trước

$style13

$style14

$style15

$style16

Sudheer tại BinaryVibes Dot Co Dot trong ¶

11 năm trước

$style17

$style18

$style12

Stan tại Dragnev Dot ca ¶

1 năm trước

$style20

$style21

$style22

$style12

Sudheer tại BinaryVibes Dot Co Dot trong ¶

Gwyneth Dot Llewelyn tại Gwynethllewelyn Dot Net ¶

$style24

$style25

$style26

$style27

$style28

$style12

1 tháng trước

1 năm trước

$style30

$style31

$style32

$style33

Sudheer tại BinaryVibes Dot Co Dot trong ¶

Gwyneth Dot Llewelyn tại Gwynethllewelyn Dot Net ¶

$style34

$style35

1 tháng trước

$style37

$style12