Format numeric column

Hi,

I have a list of products attached to the grid. The Weight column is of type "double" in the model. I need to format the Weight column with 2 decimal places. Please look at the examples below:

1 should be 1.00
3.5 should be 3.50
5.85 should be 5.85

I tried to use the format attribute in the html like format="{0:N2}" but it didn't work.

Any help would be appreciated.

Thanks,
Leo

9 Replies

UN Unknown August 5, 2018 02:20 PM UTC

Try this:    format="N2"


HJ Hariharan J V Syncfusion Team August 6, 2018 10:12 AM UTC

Hi Panora,  

Thanks for contacting Syncfusion support. 

@Jacob Thanks your suggestion. 

As per @Jacob suggestion, we need to set `N2` on the column format property. In the Essential JS2 components, we have used Internalization library(cldr data) for formatting, Please refer the below documentation links. 
 
 
Please let us know if you have any concerns. 
 
Regards,  
Hariharan  



UN Unknown August 6, 2018 02:34 PM UTC

Thanks Hariharan,

Formatting appears to work fine for columns, but I've found that it does not work on aggregate columns.  Can you add aggregate columns to your grid and try it out?  I have a float that I'm trying for format N2.


PA Panora August 7, 2018 05:43 AM UTC

Unfortunately N2 doesn't work. Is this because the field in the model is nullable double?


PA Panora August 7, 2018 05:59 AM UTC

What worked for me was to specify the type of the column using the "type" attribute.

<e-grid-column field="Rating" headerText="Rating" width="100" textAlign="Center" type="number" format="N2"></e-grid-column>

According to the documentation, if the type is not specified, it will use the first row in the grid to determine the type of the column. In my case the value for the field in the first row was null (since in my model I used nullable double), so I guess the field was translating to string type and N2 by itself didn't work. As soon as I added the type attribute, started working as expected.

Thank you for all the help.


HJ Hariharan J V Syncfusion Team August 7, 2018 10:41 AM UTC

Hi Jacob, 
 
Based on your request we have created a sample ‘Aggregate column with N2 format’. Please find the code example and sample for your reference. 
 
 
Code example 
 
 
<ejs-grid id="Grid" dataSource="ViewBag.datasource" allowPaging="true"> 
        <e-grid-aggregates> 
            <e-grid-aggregate> 
                <e-aggregate-columns> 
                    <e-aggregate-column field="Freight" format="N2" type="Sum" footerTemplate="Sum:${Sum}"></e-aggregate-column> 
                </e-aggregate-columns> 
            </e-grid-aggregate> 
            <e-grid-aggregate> 
                <e-aggregate-columns> 
                    <e-aggregate-column field="Freight" format="N2" type="Average" footerTemplate="Average:${Average}"></e-aggregate-column> 
                </e-aggregate-columns> 
            </e-grid-aggregate> 
        </e-grid-aggregates> 
        <e-grid-pagesettings pageCount="5"></e-grid-pagesettings> 
        <e-grid-columns> 
             . . . 
            <e-grid-column field="ShipCountry" headerText="Ship Country" width="140"></e-grid-column> 
        </e-grid-columns> 
    </ejs-grid> 
 
 
 
 
 
Regards, 
Hariharan 



HJ Hariharan J V Syncfusion Team August 7, 2018 10:42 AM UTC

Hi Leonid,  
  
Thanks for the feedback.  
 
We are very happy to hear that your issue is resolved. Please let us know if you have any further assistance on this.  
   
Regards,  
Hariharan 



UN Unknown August 7, 2018 03:39 PM UTC

I cannot reproduce the problem on your example, but I think a difference may be that I'm using a URLAdaptor.  Sometimes the formatting on aggregate columns works, and sometimes it doesn't.  Forcing the grid to repaint by sorting a column will make the formatting work, and sometimes a re-sort makes the formatting disappear again.


MF Mohammed Farook J Syncfusion Team August 8, 2018 05:52 AM UTC

Hi Jacob, 


We have created a sample “Url Adaptor with sorting and aggregate(with formats) ” and you can download from the following link 


Still if you have faced any issue , please share the Essential JS2 version and the Grid code example or sample if it is possible . it helps to find the solution at earlier 


Regards, 
J Mohammed Farook 


Loader.
Up arrow icon