整数只有10位,小数只有6位


Decimal number with only 10 digits for the integer and 6 for the decimals?

假设我有一个给定的数字47.50,我怎么能允许整数只有10位,小数只有6位,比如0000000047.500000或者给出另一个例子:3475.95到这个0000003475.950000

//可以使用str_pad吗?我该如何使用它?

$amount = str_pad(3475.95, 10, '0', STR_PAD_LEFT);

这不是最优雅的方法,但嘿,我们谈论的是脚本,对吧?

<?php
$value = 47.56;
echo sprintf('%s.%s', 
             str_pad((int)$value, 10, '0', STR_PAD_LEFT), 
             str_pad(substr($value-(int)$value,2), 6, '0', STR_PAD_RIGHT));
?>

抱歉,无法抗拒。但显然,马克·贝克在上述评论中指出了正确的解决方案:sprintf("%016.6f", 3475.95);。。。