3

we are using following code to display price in frontend.

 <span id="valueprice_<?php echo $products->getId(); ?>"><?php echo $products->getPrice(); ?></span>
    <input type = "text" id = "price_<?php echo $products->getId(); ?>" onkeydown="validateNumbers(event)" "name = "price" value = "<?php echo $products->getPrice(); ?>" />

in frontend price is displaying as follows. suppose if price is 100, than its displaying as 100.0000 .

after decimal points, 4 zeros [numbers] are displaying. I want to restrict to only 2 numbers after decimal point.

  • try this number_format($products->getPrice(), 2, '.', '') – Magento 2 Nov 25 '15 at 11:48
  • try using number_format($products->getPrice(), 2); – Rakesh Jesadiya Nov 25 '15 at 11:59
  • i tried this, but it did't worked : <input type = "text" id = "price_getId(); ?>" onkeydown="validateNumbers(event)" "name = "price" value = "getPrice(), 2, '.', '') ?>" style = "display:none"/> –  Nov 25 '15 at 12:11

2 Answers2

1

To get the product price with currency

Mage::helper('core')->currency($products->getPrice(), true, false);

Refer this Link

Edit :

Update your code like this

<span id="valueprice_<?php echo $products->getId(); ?>"><?php echo Mage::helper('core')->currency($products->getPrice(), true, false); ?></span> 
<input type = "text" id = "price_<?php echo $products->getId(); ?>" onkeydown="validateNumbers(event)" "name = "price" value = "<?php echo Mage::helper('core')->currency($products->getPrice(), true, false); ?>" style = "display:none"/>

Get Price without currency

<span id="valueprice_<?php echo $products->getId(); ?>"><?php echo Mage::getModel('directory/currency')->formatTxt($products->getPrice(), array('display' => Zend_Currency::NO_SYMBOL)); ?></span> 
<input type = "text" id = "price_<?php echo $products->getId(); ?>" onkeydown="validateNumbers(event)" "name = "price" value = "<?php echo Mage::getModel('directory/currency')->formatTxt($products->getPrice(), array('display' => Zend_Currency::NO_SYMBOL)); ?>" style = "display:none"/>
MeenakshiSundaram R
  • 9,577
  • 7
  • 33
  • 56
  • can you please give complete code, because i am adding this code at the top : currency($products->getPrice(), true, false); ?> but its not working –  Nov 25 '15 at 12:19
  • <?php echo $products->getPrice(); ?> this will return value right? – MeenakshiSundaram R Nov 25 '15 at 12:29
  • yes, you are right...... –  Nov 25 '15 at 12:29
  • Mage::helper('core')->currency($products->getPrice(), true, false); does this return any error? – MeenakshiSundaram R Nov 25 '15 at 12:30
  • no errors, but still 4 decimal digits are displaying..... –  Nov 25 '15 at 12:44
  • is i have to do any modification for the above code? –  Nov 25 '15 at 13:11
  • i am using code like this : currency($products->getPrice(), true, false); ?>
    getSpecialPrice(); ?> <input type = "text" id = "specialprice_getId(); ?>" onkeydown="validateNumbers(event)" "name = "price" value = "getSpecialPrice(); ?>" style = "display:none"/>
    –  Nov 25 '15 at 13:14
  • you have put echo see the out put. <?php echo Mage::helper('core')->currency($products->getPrice(), true, false); ?> – MeenakshiSundaram R Nov 25 '15 at 13:15
  • i tried this : currency($products->getPrice(), true, false); ?>
    getSpecialPrice(); ?> <input type = "text" id = "specialprice_getId(); ?>" onkeydown="validateNumbers(event)" "name = "price" value = "getSpecialPrice(); ?>" style = "display:none"/> but same result......
    –  Nov 25 '15 at 13:19
  • you need to replace the that code in value="<?php //code comes here?>" – MeenakshiSundaram R Nov 25 '15 at 13:25
  • sorry, please check now : is this right code : currency($products->getPrice(), true, false); ?>
    getPrice(); ?> <input type = "text" id = "price_getId(); ?>" onkeydown="validateNumbers(event)" "name = "price" value = "getPrice(); ?>" style = "display:none"/> but not working......
    –  Nov 25 '15 at 13:32
  • try this currency($products->getPrice(), true, false); ?> <input type = "text" id = "price_getId(); ?>" onkeydown="validateNumbers(event)" "name = "price" value = "currency($products->getPrice(), true, false); ?>" style = "display:none"/> – MeenakshiSundaram R Nov 25 '15 at 13:34
  • please post your comment as answer.... –  Nov 25 '15 at 13:36
  • i dont need currrency, i need only 2 digits.... please give me updated code –  Nov 25 '15 at 13:56
0

You have to use the currency helper like below:

<?php $price = Mage::getModel('directory/currency')->formatTxt($products->getPrice(), array('display' => Zend_Currency::NO_SYMBOL));?>
 <span id="valueprice_<?php echo $products->getId(); ?>"><?php echo $price; ?></span>
<input type = "text" id = "price_<?php echo $products->getId(); ?>" onkeydown="validateNumbers(event)" "name = "price" value = "<?php echo $price; ?>" />