How to Remove Decimal From Quantity in Magento 2 Admin Product Grid
Magento 2 CMS offers grids in the admin panel for easy data management. It offers the Products, Customers, Orders, etc. grids to manage the data of products, customers, and orders.
Today, I’ll talk about the products grid, and particularly the product quantity column in the products grid.
The Products grid in Magento 2 displays product information like product ID, thumbnail, name, product type, attribute set, SKU, price, quantity, status, etc.
Now, in the default Magento 2 CMS, the product quantity is displayed in decimal format up to 4 decimals. For example, it shows 100.0000 quantity for products with 100 quantity.
The below image shows the default format of how the product quantity is displayed in the admin product grid in Magento 2:
However, in most cases, the product quantity is always in the whole number. The integer value of the product quantity makes little sense.
Therefore, you may want to remove this unnecessary format of quantity and simply display it as 100 on the products grid in the admin panel of Magento 2.
You can remove decimals from quantity in Magento 2 admin product grid using the below code.
Programmatic Method to Remove Decimal From Quantity in Magento 2 Admin Product Grid:
- Create product_listing.xml file at app/code/Vendor/Module/view/adminhtml/ui_component
12345678910111213141516<?xml version="1.0" encoding="UTF-8"?><listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd"><columns name="product_columns" class="Magento\Catalog\Ui\Component\Listing\Columns"><column name="qty" class="Vendor\Module\Ui\Component\Listing\Columns\QtyColumn"><argument name="data" xsi:type="array"><item name="config" xsi:type="array"><item name="filter" xsi:type="string">textRange</item><item name="add_field" xsi:type="boolean">true</item><item name="label" xsi:type="string" translate="true">Quantity</item><item name="sortOrder" xsi:type="number">75</item></item></argument></column></columns></listing> - Create QtyColumn.php file at app/code/Vendor/Module/Ui/Component/Listing/Columns/
1234567891011121314151617181920212223<?phpnamespace Vendor\Module\Ui\Component\Listing\Columns;use Magento\Ui\Component\Listing\Columns\Column;class QtyColumn extends Column{const NAME = 'column.qty';public function prepareDataSource(array $dataSource){if (isset($dataSource['data']['items'])) {$fieldName = $this->getData('name');foreach ($dataSource['data']['items'] as & $item) {if (isset($item[$fieldName])) {$item[$fieldName] = (int)$item[$fieldName];}}}return $dataSource;}}
That’s it.
Once implemented, the product quantity will be displayed as shown below:
Use the above code and improve the product grid for a simpler understanding!
Any doubts in the above method can be mentioned in the Comments section below. I’d be happy to help.
Also, do share the post with Magento Community via social media.
Thank you.
Also read:
Chandresh Chauhan
He has been with Meetanshi for more than three years now as a certified Magento developer. A silent guy whom you can always find solving clients' issues, is an avid reader too.
2 Comments
bin/magento setup:upgrade
It occurs an error below.
Cannot process definition to array for type mediumint
After applying this solution are you unable to run setup:upgrade command ?