How can I insert to a string comma (,) after every 3 position starting from last?
input desired output
9876567846678 9,876,567,846,678
567 567
1234 1,234
I used one method like first divide the whole string by three position and then while merging appending coma by three three position.
Is there any better solution? Is there function available to do this trick in java?
Edit :
LAS_VEGAS answer will format the number based on the locale. But i want to format it to a fixed locale …
how can i do tht?
Yes, there is a “trick” in Java:
EDIT:
In case you want your output to be independent from locale you can use the method below. It starts from the end so there is no need to reverse the string and should be quite efficient: