NumberFormatter::setPattern

numfmt_set_pattern

(PHP 5 >= 5.3.0, PHP 7, PHP 8, PECL intl >= 1.0.0)

NumberFormatter::setPattern -- numfmt_set_patternSet formatter pattern

说明

面向对象风格

public NumberFormatter::setPattern ( string $pattern ) : bool

过程化风格

numfmt_set_pattern ( NumberFormatter $fmt , string $pattern ) : bool

Set the pattern used by the formatter. Can not be used on a rule-based formatter.

参数

fmt

NumberFormatter object.

pattern

Pattern in syntax described in » ICU DecimalFormat documentation.

返回值

成功时返回 true, 或者在失败时返回 false

范例

Example #1 numfmt_set_pattern() example

<?php
$fmt 
numfmt_create'de_DE'NumberFormatter::DECIMAL );
echo 
"Pattern: ".numfmt_get_pattern($fmt)."\n";
echo 
numfmt_format($fmt1234567.891234567890000)."\n";
numfmt_set_pattern($fmt"#0.# kg");
echo 
"Pattern: ".numfmt_get_pattern($fmt)."\n";
echo 
numfmt_format($fmt1234567.891234567890000)."\n";
?>

Example #2 OO example

<?php
$fmt 
= new NumberFormatter'de_DE'NumberFormatter::DECIMAL );
echo 
"Pattern: ".$fmt->getPattern()."\n";
echo 
$fmt->format(1234567.891234567890000)."\n";
$fmt->setPattern("#0.# kg");
echo 
"Pattern: ".$fmt->getPattern()."\n";
echo 
$fmt->format(1234567.891234567890000)."\n";
?>

以上例程会输出:

Pattern: #,##0.###
1.234.567,891
Pattern: #0.# kg
1234567,9 kg

参见

User Contributed Notes

Crash 02-Oct-2020 10:27
If you're looking for the pattern syntax, their documentation is here: http://unicode.org/reports/tr35/tr35-numbers.html#Number_Format_Patterns
Anonymous 06-Jun-2019 02:22
setPattern appears to influence only the behavior of the format method, not the parse method of the NumberFormatter object.

$pattern = '#,##0';
$frmtr = NumberFormatter::create('en-US', NumberFormatter::DECIMAL);
$frmtr->setPattern($pattern);
echo $frmtr->parse('12.345');

// returns 12