# How to get decimal value of weight in mini cart?

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

• 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
• Yeah :D But, it's knockout. So, It's different que than I gave all answer. Oct 15, 2018 at 9:07

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.

• 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

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

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

JS:

``````var value = 11.0000;
var result = value.toFixed(2);
``````

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);
</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 Oct 15, 2018 at 8:06
• Check my update, I don't tested it but I think it should work 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 Oct 15, 2018 at 8:41
• I am not getting any value as update2 – PRINCE Oct 15, 2018 at 8:46