Custom number separators (thousand and decimal)

Hello,

Is there a way to set custom thousand and decimal separators in numeric columns in Grid without changing Locale altogether?
I know that you can set Locale in grid attributes and separators will be set according to that, but I want to have custom separators, every user of my application has set his own preference on separators which is not dependent on culture. So for users that have the same culture, some have set comma separator and others dot separator for decimals and so on for thousand separator.
When those users load a page that has a Grid with numeric data in it, I want the data to be shown according to their preferred separator.
Is there a way to use the column format or something similar to achieve that?
Thank you.

Best regards

5 Replies 1 reply marked as answer

RS Rajapandiyan Settu Syncfusion Team June 11, 2021 06:39 AM UTC

Hi Spyros,   
  
Thanks for contacting Syncfusion support.  
  
Based on your query, you want to apply a different format on the number value without changing the locale of the Grid component. You can achieve your requirement by using valueAccessor property of Grid Column.  
  
By using valueAccessor, you can customize the values as you want and the returned data will be shown in Grid. Please refer to the below code example and sample for more information.  
  
  
  
[index.js]  
  
  
export class Default extends SampleBase {  
  valueAccess(fielddatacolumn) {  
    var value = data[column.field]; // get the value of column  
    if (data['OrderID'] % 2 == 0) { // use the condition as you want  
      // format the number with decimal separator as dot  
      return new Intl.NumberFormat('en-US', { minimumFractionDigits: 2 }).format(value);  
    } else {  
      // format the number with decimal separator as comma  
      return new Intl.NumberFormat('de-DE', { minimumFractionDigits: 2 }).format(value);  
    }  
  }  
  render() {  
    return (  
      <GridComponent dataSource={orderDetails} height="350">  
        <ColumnsDirective>  
          <ColumnDirective field="OrderID" headerText="Order ID" width="120" />  
          <ColumnDirective  
            field="Freight"  
            headerText="Freight"  
            width="120"  
            valueAccessor={this.valueAccess.bind(this)}  
          />  
        </ColumnsDirective>  
      </GridComponent>  
    );  
  }  
}  
  
  
   
  
Note:  
By using valueAccessor, you can return the customized data as you want. But, this customized data used only for the display purpose. All the Grid’s data operations like Sorting, Filtering, Searching, etc., are performed based on the value in the Grid’s dataSource. This is the behavior of Grid.  
  
Please get back to us if you need further assistance with this.  
  
Regards,  
Rajapandiyan S  



SP Spyros June 11, 2021 07:12 AM UTC

Dear Rajapandiyan S,

Thank you for your reply, I was not aware about the valueAccessor property!
From what I see in the provided example, you are again using locale (en-US, de-DE) in there. It gets the job done, although I was wondering if there is any way to do that without tying locale. What I have from users is just the dot, comma symbol, so with your solution I need to make a function that ties all possible separator symbols to the respective locale that uses them. The ideal solution was to use the symbol itself somehow into valueAccessor property. With that I mean just the "," or "." or " " etc. Is that possible?

Looking forward to your reply,
Best regards!


RS Rajapandiyan Settu Syncfusion Team June 14, 2021 01:22 PM UTC

Hi Spyros,    
 
Thanks for your update. 
 
Query: I was wondering if there is any way to do that without tying locale. What I have from users is just the dot, comma symbol, so with your solution I need to make a function that ties all possible separator symbols to the respective locale that uses them. 
 
We have validated your requirement at our end. You can achieve this by using the following code example in the valueAccessor method. 
 
 
[index.js] 
 
 
  valueAccess(field, data, column) { 
    var value = data[column.field]; 
    if (data['OrderID'] % 2 == 0) { 
      var parts = value.toString().split('.'); 
      parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g','); 
      return parts.join('.'); 
    } else { 
      var parts = value.toString().split('.'); 
      parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g'.'); 
      return parts.join(','); 
    } 
  } 
 
 
 
 
Find the below online documentation for your reference, 
 
Please get back to us if you need further assistance with this.   
   
Regards,   
Rajapandiyan S   


Marked as answer

SP Spyros June 15, 2021 12:04 PM UTC

Dear Rajapandiyan S,

Thank you for the provided solution and example code, it works great!
Have a great day!

Best regards,
Spyros


RS Rajapandiyan Settu Syncfusion Team June 16, 2021 03:42 AM UTC

Hi Spyros,  
  
You’re welcome. We are glad that the provided solution resolved your requirement. 
 
Please get back to us if you need further assistance with this. 

Regards, 
Rajapandiyan S 


Loader.
Up arrow icon