我正在编写一个脚本,需要将布局格式化为3 numbers ( 000 )....当我返回我的SQL时,我得到的结果可能是'1',可能是'100',可能是'50'它可能是任何地方在0到999之间....我怎么能总是让它作为3号布局返回.
例如,如果结果返回1,则需要读取001,如果返回50,则需要读取050,依此类推.
有这个sprintf()功能:
$formatted_decimal = sprintf('%03d', $unformatted_decimal);
# 1 -> 001, 10 -> 010, etc...
Run Code Online (Sandbox Code Playgroud)
一般提示:除非您有非常具体的理由,否则不要存储格式化版本.输出格式很便宜.如果您稍后更改格式要求,存储"原始"数据几乎总是更好的选择.