function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Developer_shaanDeveloper_shaan 

Insertion of comma after evry third character in a string

I have a string which has value as "24098909090" .Now i need to display the value in thousands.

i.e after every third charcter in a string a comma should be inserted.

Ex: "24,098,909,090" .

 

Is there any solution for this....

How to make use of Javascript or Apex script to handle this kind of issue??

 

Thanks

shaan

Best Answer chosen by Admin (Salesforce Developers) 
SteveBowerSteveBower

 

In regular expressions, look at grouping and lookahead.

 

 

string num = '123456789';
string regex = '(\\d)(?=(\\d{3})+$)';

system.assertEquals('123,456,789', num.replaceAll(regex, '$1,'));

 

Best, Steve.

All Answers

SteveBowerSteveBower

 

In regular expressions, look at grouping and lookahead.

 

 

string num = '123456789';
string regex = '(\\d)(?=(\\d{3})+$)';

system.assertEquals('123,456,789', num.replaceAll(regex, '$1,'));

 

Best, Steve.

This was selected as the best answer
Developer_shaanDeveloper_shaan

Excellent Steve.

Thats was the best soln.Instead i was wasting my time on javascript.

 

Thats was so simple to use.

Can you please let me know how to write this expressions or else any docs related on this regular expressions.

 

 

aballardaballard

Depending on where/how you want to do this, you may also be able to just get this kind of formatting done automatically by VF using outputText's formatting capabilities.   See

http://www.salesforce.com/us/developer/docs/pages/index_Left.htm#StartTopic=Content/pages_compref_outputText.htm

 

 

SteveBowerSteveBower

As an aside, I actually agree with the poster who says that you should use currency fields if you're manipulating currency.  However that wasn't what you asked, so I didn't go there.

 

I'm not sure what you're asking about how to write the expression?   There are a zillion, literally one zillion, or perhaps a bazillion, articles, blogs, tutorials, etc. about how to use Regular Expressions.  I don't have one offhand that I can point you do, just google them and you'll find more than you want.  There's entire books as well.

 

Best, Steve.