數字輔助函數?
在本地化識別風格里數字輔助函數文件包含的函數幫助你與數字化的數據工作。
通用函數?
下面的函數是通用的:
-
number_to_size
($num[, $precision = 1[, $locale = null])? 參數: - $num (mixed) – 字節的數目
- $precision (int) – 浮點精確度
返回: 格式化數據大小 string, 要不然如果提供的值不是數字的則是錯誤的
返回類型: string
像字節一樣格式化數字,以大小為基礎,并添加適事例當的詞尾。事例:
echo number_to_size(456); // 返回 456 Bytes echo number_to_size(4567); // 返回 4.5 KB echo number_to_size(45678); // 返回 44.6 KB echo number_to_size(456789); // 返回 447.8 KB echo number_to_size(3456789); // 返回 3.3 MB echo number_to_size(12345678912345); // 返回 1.8 GB echo number_to_size(123456789123456789); // 返回 11,228.3 TB
第二個可選的參數允許你設置結果的精確度:
echo number_to_size(45678, 2); // 返回 44.61 KB
第三個可選的參數當產生數字時應該常被使用,并能對格式化產生作用,它允許你去具體指定區域。 如果沒有區域被具體指定,請求將會被解析并且適當區域會減少頭文件或者本地應用默認程序:
// 產生 11.2 TB echo number_to_size(12345678912345, 1, 'en_US'); // 產生 11,2 TB echo number_to_size(12345678912345, 1, 'fr_FR');
注解
由本段函數產生文本在接下來的語言文件中被找到: language/<your_lang>/Number.php
-
number_to_amount
($num[, $precision = 1[, $locale = null])? 參數: - $num (mixed) – 數字格式
- $precision (int) – 浮點精確度
- $locale (string) – 為了格式化區域使用
返回: string 的可讀版本, 要不然如果提供的值不是數字的為錯誤的
返回類型: string
為了計數能達到百萬的四次方,轉換數字格式為人類可讀版本,像 123.4 trillion. 事例:
echo number_to_amount(123456); // 返回 123 thousand echo number_to_amount(123456789); // 返回 123 million echo number_to_amount(1234567890123, 2); // 返回 1.23 trillion echo number_to_amount('123,456,789,012', 2); // 返回 123.46 billion
一個可選擇的第二參數允許你去設置結果的精確度:
echo number_to_amount(45678, 2); // 返回 45.68 thousand
一個可選擇的第三參數允許區域被具體指定:
echo number_to_amount('123,456,789,012', 2, 'de_DE'); // 返回 123,46 billion
-
number_to_currency
($num, $currency[, $locale = null])? 參數: - $num (mixed) – 數字格式
- $currency (string) – 貨幣類型, 例如 USD, EUR, 等等
- $locale (string) – 為了格式化區域使用
- $fraction (integer) – Number of fraction digits after decimal point
返回: 為了本地化數字應與貨幣相稱
返回類型: string
在公用的通貨格式里轉換數字, 例如 USD, EUR, GBP, 等等:
echo number_to_currency(1234.56, 'USD'); // 返回 $1,234.56 echo number_to_currency(1234.56, 'EUR'); // 返回 £1,234.56 echo number_to_currency(1234.56, 'GBP'); // 返回 £1,234.56 echo number_to_currency(1234.56, 'YEN'); // 返回 YEN1,234.56
-
number_to_roman
($num)? 參數: - $num (string) – 想要轉換的數字
返回: 來自賦予參數的被轉換的 roman 數字
返回類型: string
轉換數字為 roman:
echo number_to_roman(23); // 返回 XXIII echo number_to_roman(324); // 返回 CCCXXIV echo number_to_roman(2534); // 返回 MMDXXXIV
函數僅處理 1 到 3999 之間的數字。 超出范圍的任何值它將返回空。