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

  • you want weight value as like 11.00, right ? – Aditya Shah Oct 15 at 7:33
  • yeah its script had written in knockoutjs Aditya Shah – trilok kumar Oct 15 at 8:14
  • Can you please add your phtml & js file code here ? – Rohan Hapani Oct 15 at 8:52
  • <strong class="product-item-Weight"> <!-- ko if: weight --> <div class="Approx-Weight" data-bind="html: 'Approx Weight : ' + weight * qty "></div> <!-- /ko --> </strong> – trilok kumar Oct 15 at 8:54
  • 1
    Yeah :D But, it's knockout. So, It's different que than I gave all answer. – Rohan Hapani Oct 15 at 9:07
up vote 2 down vote accepted

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.

  • 1
    +1 :) i knew you will find – Aditya Shah Oct 15 at 9:19
  • Where should i create getWeight() function in your knockout file inside Component.extend Rohan Hapani – trilok kumar Oct 15 at 9:23
  • You can add anywhere inside Component.extend. It should be like this : return Component.extend({ defaults:{ ........... }, getWeight : function(precision,WeightValue){ return WeightValue / Math.pow(10, self.precision()); }, .............. }); – Rohan Hapani Oct 15 at 9:26
  • Happy to help :) Happy Coding !! – Rohan Hapani Oct 15 at 9:26
  • Its not working for me Rohan Hapani – trilok kumar Oct 15 at 9:39

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

number_format($weight, 2, '.', '');
  • script had written in knockoutjs Shashank Kumrawat – trilok kumar Oct 15 at 8:13

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> 
    
  • script had written in knockoutjs PRINCE – trilok kumar Oct 15 at 8:06
  • Check my update, I don't tested it but I think it should work – PRINCE Oct 15 at 8:25
  • web/template/somename.html my file location <div class="name-class" data-bind="html:js_block"></div> here how to get PRINCE – trilok kumar Oct 15 at 8:28
  • Look my update2 – PRINCE Oct 15 at 8:41
  • I am not getting any value as update2 – PRINCE – trilok kumar Oct 15 at 8:46

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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