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

8
  • you want weight value as like 11.00, right ? Oct 15, 2018 at 7:33
  • yeah its script had written in knockoutjs Aditya Shah Oct 15, 2018 at 8:14
  • Can you please add your phtml & js file code here ? Oct 15, 2018 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> Oct 15, 2018 at 8:54
  • 1
    Yeah :D But, it's knockout. So, It's different que than I gave all answer. Oct 15, 2018 at 9:07

3 Answers 3

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.

8
  • Where should i create getWeight() function in your knockout file inside Component.extend Rohan Hapani Oct 15, 2018 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()); }, .............. }); Oct 15, 2018 at 9:26
  • Happy to help :) Happy Coding !! Oct 15, 2018 at 9:26
  • Its not working for me Rohan Hapani Oct 15, 2018 at 9:39
  • Any error or log or console error? Please check it that html and js changes apply or not. Oct 15, 2018 at 9:43
1

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

number_format($weight, 2, '.', '');
1
  • script had written in knockoutjs Shashank Kumrawat Oct 15, 2018 at 8:13
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> 
    
5
  • script had written in knockoutjs PRINCE Oct 15, 2018 at 8:06
  • Check my update, I don't tested it but I think it should work
    – PЯINCƎ
    Oct 15, 2018 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 Oct 15, 2018 at 8:28
  • Look my update2
    – PЯINCƎ
    Oct 15, 2018 at 8:41
  • I am not getting any value as update2 – PRINCE Oct 15, 2018 at 8:46

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.