2024 年日本 PHP 研討會

NumberFormatter::parse

numfmt_parse

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

NumberFormatter::parse -- numfmt_parse解析數字

說明

物件導向風格

public NumberFormatter::parse(字串 $string, 整數 $type = NumberFormatter::TYPE_DOUBLE, 整數 &$offset = null): 整數|浮點數|false

程序式風格

numfmt_parse(
    NumberFormatter $formatter,
    字串 $string,
    整數 $type = NumberFormatter::TYPE_DOUBLE,
    整數 &$offset = null
): 整數|浮點數|false

使用目前的格式器規則將字串解析為數字。

參數

formatter

NumberFormatter 物件。

string

要解析數字的字串。

type

要使用的格式類型。預設使用 NumberFormatter::TYPE_DOUBLE。請注意,NumberFormatter::TYPE_CURRENCY 不受支援;請改用 NumberFormatter::parseCurrency()

offset

開始解析的字串中的偏移量。返回時,此值將保存解析結束的偏移量。

回傳值

已解析數字的值,或錯誤時返回 false

範例

範例 #1 numfmt_parse() 範例

<?php
$fmt
= numfmt_create( 'de_DE', NumberFormatter::DECIMAL );
$num = "1.234.567,891";
echo
numfmt_parse($fmt, $num)."\n";
echo
numfmt_parse($fmt, $num, NumberFormatter::TYPE_INT32)."\n";
?>

範例 #2 物件導向範例

<?php
$fmt
= new NumberFormatter( 'de_DE', NumberFormatter::DECIMAL );
$num = "1.234.567,891";
echo
$fmt->parse($num)."\n";
echo
$fmt->parse($num, NumberFormatter::TYPE_INT32)."\n";
?>

上述範例會輸出:

1234567.891
1234567

另請參閱

新增註記

使用者貢獻的註記 2 則註記

rdohms at php dot net
12 年前
值得注意的是,此函式的預期行為可能會根據您的 ICU 版本而改變。

在 ICU 4.4.2 (Ubuntu 10.* 搭配 PHP 5.3.5 的標準)

使用地區設定 'en',輸入 100,1 會回傳 1001

在 ICU 4.8.1 (Ubuntu 12.* 搭配 PHP 5.3.10 的標準)

使用地區設定 'en',輸入 100,1 會回傳 "false"

請務必在 phpinfo() 中記下您的 ICU 版本,以確保您會得到預期的輸出。
Rakasch
5 年前
'en_EN'

基本上第一部分是語言,第二部分是地區
'en_EN' - 英文,英國
'en_US' - 英文,美國

您可以在這裡查詢語言標籤,例如 'en_EN'
https://datahub.io/core/language-codes
請參閱 "ietf-language-tags"
To Top