1

How to get decimal value of weight in mini cart. I need to display only 2 decimals 11.00 instead of 11.0000

enter image description here

enter image description here

enter image description here

trilok kumar
  • 427
  • 2
  • 10
  • 21

3 Answers3

2

For that, You need to add one custom function and pass your precision value and weight value. Add this below code in your html file :

<strong class="product-item-Weight">
<!-- ko if: weight -->
    <div class="Approx-Weight" data-bind="html: 'Approx Weight : ' + getWeight(2,weight * qty) ">
    </div>
<!-- /ko -->
</strong>

Now, create getWeight() function in your knockout file inside Component.extend :

getWeight : function(precision,WeightValue){
    var self = this;
    return WeightValue / Math.pow(10, self.precision());
},

Now, refresh your html and knockout js file and check it.

Hope, It maybe helpful for you.

Rohan Hapani
  • 17,388
  • 9
  • 54
  • 96
1

you can use number_format function like this in minicart template file-

number_format($weight, 2, '.', '');
Shashank Kumrawat
  • 2,110
  • 23
  • 61
1

JS:

var value = 11.0000;
var result = value.toFixed(2); 
alert(result); // 11.00

PHP:

$value  = "11.0000";
$result = bcadd($value , 0, 2);

echo $result ; // 11.00

Update:

You can create a block, you put your js inside, then you bind that block in your knockoutjs

1. <?php $jsBlock = echo $this->getLayout()->createBlock("Magento\Framework\View\Element\Template")->setTemplate("html/custom.phtml")->toHtml(); ?>

<script type="text/javascript">
    var js_block = <?php echo json_encode($jsBlock)?>;
</script>
2. web/template/somename.html
<div class="name-class" data-bind="html:js_block"></div> 

3. custom.phtml 
<script type="text/javascript">
    var value = 11.0000;
    var result = value.toFixed(2); 
    alert(result); // 11.00
</script>

Update2:

  1. app/design/frontend/{Vendor}/{theme}/Magento_Checkout/view/frontend/templates/cart/minicart.phtml

    <?php $jsBlock = echo $this->getLayout()->createBlock("Magento\Framework\View\Element\Template")->setTemplate("html/custom.phtml")->toHtml(); ?>
    
    <script type="text/javascript">
        var js_block = <?php echo json_encode($jsBlock)?>;
    </script>
    
  2. web/template/somename.html

    <div class="name-class" data-bind="html:js_block"></div> 
    
PЯINCƎ
  • 11,669
  • 3
  • 25
  • 80