3

How does one round the value of a weight down to a whole number? Currently my product pages show weight with many digits after the decimal point: enter image description here

I want it to be like so: enter image description here

This is my code in price.phtml:

<span class="regular-weight"> <?php $pro=Mage::getModel('catalog/product')->load($_product->getEntityId()); ?>
                 <?php echo  $pro->getWeight(); ?><?php echo $this->__('grams');?>
            </span>
1

4 Answers 4

3

Try this :

<span class="regular-weight">
     <?php echo number_format($_product->getWeight())?><?php echo $this->__('grams');?>
</span>

OR

<span class="regular-weight">
     <?php echo round($_product->getWeight())?><?php echo $this->__('grams');?>
</span>
3
$_MyValue = $pro->getWeight();
if (is_numeric($_MyValue))
{
 $my_data_value = number_format((float)$_MyValue, 0, '.', '');
}
2
<span class="regular-weight"> <?php $pro=Mage::getModel('catalog/product')->load($_product->getEntityId()); ?>
    <?php    
        $weight = (float) $pro->getWeight();
        $formattedWeight = number_format($weight, 0, ',', '');
        echo sprintf("%s %s", $formattedWeight, $this->__('grams'));
    ?>
</span>
2

In database, weight attribute have backend_type set to decimal.

To change the decimal precision of attributes that have a backend_type set to decimal,

You have to change the type of value in this table :

catalog_product_entity_decimal

If you have not change yet, you should see :

decimal(12,4)

In your case, you have to replace with

decimal(12,0)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.