我有一个小问题....
我有一个php函数,它基于除以两个整数生成一个数字。这将始终表示为 1 的百分比。
例如,我的函数将生成一个数字,例如 0.3 或 0.01222 或 0.23
我需要将此百分比转换为整数,作为三位数字的 100 的一部分。
所以 0.3 会变成 030,0.01222 会变成 001,1 会变成 100
有没有一种简单的方法可以在 PHP 中做到这一点?
干杯:)
最终结果将不再是一个数字,而是一个字符串,所以只需使用字符串操作:
$number = 0.3;
// Multiply by 100 and remove stuff after decimal point
$string = (string) floor($number * 100);
// Add zeros until the string has a total length of three characters
$string = str_pad($string, 3, '0', STR_PAD_LEFT);
是的,有:
$num = 0.01222;
$num2 = ceil($num * 100);