Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 为什么小数点后两位的尾随零没有打印出来?_Php - Fatal编程技术网

Php 为什么小数点后两位的尾随零没有打印出来?

Php 为什么小数点后两位的尾随零没有打印出来?,php,Php,我正在使用Github中的一个类进行字符串操作,以便在热敏打印机上打印出来。这是上课时间 class item { private $name; private $price; private $dollarSign; public function __construct($name = '', $price = '', $dollarSign = false) { $this -> name = $name; $

我正在使用Github中的一个类进行字符串操作,以便在热敏打印机上打印出来。这是上课时间

class item
{
    private $name;
    private $price;
    private $dollarSign;

    public function __construct($name = '', $price = '', $dollarSign = false)
    {
        $this -> name = $name;
        $this -> price = $price;
        $this -> dollarSign = $dollarSign;
    }

    public function __toString()
    {
        $rightCols = 10;
        $leftCols = 38;
        if ($this -> dollarSign) {
            $leftCols = $leftCols / 2 - $rightCols / 2;
        }
        $left = str_pad($this -> name, $leftCols) ;
6y
        $sign = ($this -> dollarSign ? 'RM ' : '');
        $right = str_pad($sign . $this -> price, $rightCols, ' ', STR_PAD_LEFT);
        return "$left$right\n";
    }
}

这里的问题是,我仍然在打印价格时没有尾随0。例如,输入是4.50,而类的返回结果(return“$left$right\n”;)只有4.5。输入6.00也是一样,我只得到6。小数点消失后,任何人都可以共享尾随的零?不使用该类,我可以成功打印出4.50和6.00。只有在使用该类后,我才发现这个问题。

保证始终在美元金额后显示美分

使用时,它将保证小数点后两位

number_format($the_price, 2);

STR_PAD_LEFT你确定吗?是的,只是为了确保数字在纸张的左侧。嗨,斯蒂芬,我想以字符串形式打印到热敏打印机上,因为我知道sprintf已经是一个打印命令。这是同一件事吗?是的,但一旦使用类作为价格输入,尾随的0就消失了。
number_format($the_price, 2);