仅舍入变量的整数


Round only int of variable

我正忙于网上商店,我有一个问题:

显示产品的数量和产品的属性。

喜欢:

2,4 cm
2,56 dm

现在我想对变量的数字进行四舍五入,但是如果我使用:

$qty = round($_product->getBundle_qty());

但是如果我使用:$aantal = $_product->getBundle_qty();一切正常,但他不圆。

然后属性不再显示(dm,cm,m ETC)

有人可以帮助我吗?

$_product->getBunlde_qty() 实际上是双精度还是浮点数?还是只是一根绳子?在这种情况下,您可以使用:

$qty = round(floatval($_product->getBundle_qty()));

编辑:现在,如果您想将cm/dm保留在字符串后面,我们可以获取字符串并在所有空格处将其分解,从而从字符串创建一个数组。此数组中的最后一个值将是"cm"或"dm"文本。

$qtyPieces = explode(" ", $_product->getBundle_qty()); //explode at the space
$qtyPiecesCount = count($qtyPieces); //count all pieces
$qty = round(floatval($_product->getBundle_qty())) . " " . $qtyPieces[$qtyPiecesCount - 1]; //get the last value in the array, count minus 1 because we start counting from 0

使用爆炸修复了它:

                            $arr = explode(' ',trim($_product->getBundle_qty()));
                            echo $arr[1];

这是因为属性部分是一个字符串。

  1. 首先对整数进行舍入。

     //example:
     $qty = round(2.4);
    
  2. 然后将字符串属性添加回其中。

     $value = $qty . "cm";