PHP 数字格式 4 位

标签 php numbers number-formatting

<分区>

例如,我有这样的格式: $s_number = "12"; 对于这个输入,我需要 0012 作为输出:

另一个例子:

输入$s_number = "3"; 输出:0003

每次我需要 4 位数字时,我都希望发生这种情况。

最佳答案

它不会是数字(而是字符串),但您可以使用 str_pad 来实现。在你的例子中:

$s_number = str_pad( "12", 4, "0", STR_PAD_LEFT );
$s_number = str_pad( "3", 4, "0", STR_PAD_LEFT );

关于PHP 数字格式 4 位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15234368/

相关文章:

php - 不使用 PHP 重定向的 POST

php - Magento 模块中的 Mydomain 错误

javascript - 禁用按钮,直到达到一个数字

c++ - 解析数字字符串

php - 共享主机上的 Websockets : not possible because of shared hosting itself or because of non-php integrations?

php - 分解字符串以获取关键字(Youtube channel 关键字)

c - 在 C 中打印数字行

sql - Netezza 格式化函数

r - 在 R 中将整数 7 作为字符 "007"写入

javascript - JS Intl 符号后的空格