1

I have the following code in my list.phtml file, this pulls in the price of the product and then multiplies it by 1.20 (this works great but it displays 3 or 4 numbers after the decimal point

 <?php if($_product->getData('deals_bus_price')) : ?>
    <?php  $attribute = $_product->getResource()->getAttribute('deals_bus_price');
if ($attribute) { echo "£", $attribute_value = $attribute ->getFrontend()->getValue($_product)*1.20 ; } ?>


<?php endif; ?>

Is there any way that I can add something which forces it to display only 2 decimals after the point..

I have tried getValue($_product)*1.20, 2; etc but that doesn't work, all it displays is £288.492

  • 1
    try echo Mage::helper('checkout')->formatPrice($attribute_value); – Qaisar Satti Jul 25 '16 at 9:52
  • @plugger sir ...please check answer.. If you are not satisfy with answer then you can tell me. :) Thanks . – Rohan Hapani Aug 29 '17 at 4:37
  • @plugger... sir ...if answer is useful then you can approve answer and upvote it....so other people can also use this code to solve their issue. Thanks. – Rohan Hapani Sep 1 '17 at 18:43
6

Try this core helper function :

// This will show a price with Currency symbol
Mage::helper('core')->currency(PRICE_HERE, true, false);

Otherwise use number_format :

number_format(PRICE_HERE, 2, '.', '');
number_format($products->getPrice(), 2, '.', '') ;

The complete code:

<?php if($_product->getData('deals_bus_price')) : ?>
<?php 
    $attribute = $_product->getResource()->getAttribute('deals_bus_price');
    if ($attribute) {
        $attribute_value = $attribute->getFrontend()->getValue($_product)*1.20 ; 
        echo Mage::helper('core')->currency($attribute_value, true, false);
    } 
?>
<?php endif; ?>
1

You can use php's number_format() function.

This function returns a string.

  • You can user number_format function for price. – Ravi Thanki Jul 25 '16 at 10:33
1

Try this

<?php if($_product->getDealsBusPrice()) : ?>
<?php  $attributeValue = $_product->getAttributeText('deals_bus_price');?>
<?php $attributeValue = ($attributeValue * 1.20)?>
<?php $attributeValue =  round($attributeValue, 2); ?>
<?php echo "£".$attributeValue; ?>
<?php endif; ?>

OR

<?php if($_product->getDealsBusPrice()) : ?>
<?php  $attributeValue = $_product->getAttributeText('deals_bus_price');?>
<?php $attributeValue = ($attributeValue * 1.20)?>
<?php echo Mage::helper('checkout')->formatPrice($attributeValue);?>
<?php endif; ?>
1

Use this code for 2 decimal places :

<?php if($_product->getData('deals_bus_price')) : ?>
    <?php  $attribute = $_product->getResource()->getAttribute('deals_bus_price');
if ($attribute) { echo "£", $attribute_value = number_format($attribute ->getFrontend()->getValue($_product)*1.20,2) ; } ?>


<?php endif; ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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