如何将数字(价格)从1.00
转换为000000000100
总计所有字符串必须12 digits
如果价格100.00
那么函数应该转换为相同的000000010000
如果价格150.00
函数应该转换为000000015000
。
这个值实际上我需要发送到银行(支付网关)进行处理。
谢谢,如果你能帮我这个.
这里有一个函数可以做到这一点:
/**
* Format a price according to the bank payment gateway specification.
*
* @param $price float The price
* @return The formatted price
*/
function format_price($price) {
$price = (int) ($price * 100);
return str_pad((string) $price, 12, '0', STR_PAD_LEFT);
}
你可以
试试这个。
$num=<number with 2 decimal places>;
$strnum=''.intval($num*100);
while(strlen($strnum)<12)
$strnum='0'.$strnum;
那些从 c dev 来到 php dev 的人的答案:)
$a = 100.00;
printf("a=%012d", $a*100);
输出:a=000000010000
$a = 150.00;
printf("a=%012d", $a*100);
输出:a=000000015000
对于数字转换需要使用sprintf
:
$a = 150.00;
$b = sprintf("%012d", $a*100);
var_dump($b);
输出: 字符串(12) "000000015000"