+ Start a Discussion
gowtham murugesangowtham murugesan 

how to save a Data( Number) include space ?

Hi all,

I have a custom field called "Amount" 
this my input "123456"
But need to display as "123  456" like this format.
How to include space in this using trigger?
thanks in advance 
Best Answer chosen by gowtham murugesan
Ajay K DubediAjay K Dubedi
Hi Gowtham,

If You are using a text field than to create space in a value use below code. This code will create space between charaters of string in half of the length of String.
for eg:
For 6 digit length of string : Space will be after 3 charaters.
For 8 digit length of string : Space will be after 4 charaters.
For 7 digit length of string : Space will be after 3 charaters.
 
String str= '123456';
Integer n = str.length();
String Newstr = '';
for(Integer i = 0; i <n;i++ )
{
    Newstr+= str.substring(i,i+1);
    if(i == n/2-1){
        Newstr+= ' ';
    } 
}
System.debug('Newstr');

Now you can assign this value to any text type field.
Thanks,
Ajay Dubedi

All Answers

Ajay K DubediAjay K Dubedi
Hi Gowtham,

A number type field cannot contain space between digits even leading zeros also removed.
If we will force to insert it as containing space it will generate an error:

System.TypeException: Invalid integer:
My suggestion is to create your field as a text type.

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
gowtham murugesangowtham murugesan
Hi Ajay,

Here the field datatype is "text" 
Ajay K DubediAjay K Dubedi
Hi Gowtham,

If You are using a text field than to create space in a value use below code. This code will create space between charaters of string in half of the length of String.
for eg:
For 6 digit length of string : Space will be after 3 charaters.
For 8 digit length of string : Space will be after 4 charaters.
For 7 digit length of string : Space will be after 3 charaters.
 
String str= '123456';
Integer n = str.length();
String Newstr = '';
for(Integer i = 0; i <n;i++ )
{
    Newstr+= str.substring(i,i+1);
    if(i == n/2-1){
        Newstr+= ' ';
    } 
}
System.debug('Newstr');

Now you can assign this value to any text type field.
Thanks,
Ajay Dubedi
This was selected as the best answer
gowtham murugesangowtham murugesan
Thankyou Ajay,The above code is working Fine.