所以我想显示一个包含在标签中的每个字符的计数器,并且还要添加一个数千个分隔符。
该号码必须至少为3个字符。
案例1:
我拿了一个号码12
代码:
$number = 12 //number 12
$number = number_format($number, 0, '', ','); //add a thousand seprator
$number = str_pad($number,3,0,STR_PAD_LEFT); //pad if length less than 3
$counter = "";
foreach (str_split($number) as $key => $digit) {
$counter .= '<b>' . $digit . '</b>';
}
输出:012
(忽略<b>
标记)
案例2:
我拿了一个号码1234
代码:
$number = 1234 //number 1234
$number = number_format($number, 0, '', ','); //add a thousand seprator
$number = str_pad($number,3,0,STR_PAD_LEFT); //pad if length less than 3
$counter = "";
foreach (str_split($number) as $key => $digit) {
$counter .= '<b>' . $digit . '</b>';
}
输出:1,234
(忽略<b>
标记)
案例3:
我拿了一个号码0012
代码:
$number = 0012 //number 0012
$number = number_format($number, 0, '', ','); //add a thousand seprator
$number = str_pad($number,3,0,STR_PAD_LEFT); //pad if length less than 3
$counter = "";
foreach (str_split($number) as $key => $digit) {
$counter .= '<b>' . $digit . '</b>';
}
输出:010
(忽略<b>
标记)
那么案例3 出了什么问题?
更新
如何避免这种情况?
答案 0 :(得分:4)
答案是,因为八进制:
$number = 0012;
0
前缀导致PHP使用八进制到十进制转换来转换12
;和12八进制== 10十进制。
为避免这种情况,您可以改为使用字符串值:
$number = '0012';
另请参阅:Integer notation