Add a separator of thousand points

Source: Internet
Author: User

+ (nsstring*) Strmethodcomma: (nsstring*) string
{

Nsstring*sign = nil;
if ([stringhasprefix:@ "-"]| | [String hasprefix:@ "+"]) {
sign = [string substringtoindex:1];
string = [string substringfromindex:1];
}

Nsstring*pointlast = [string substringfromindex:[string length]-3];
Nsstring*pointfront = [string substringtoindex:[string length]-3];

int commanum= ([Pointfront length]-1)/3;
Nsmutablearray *arr = [Nsmutablearray array];
for (int i =0; i < commanum+1; i++) {
int index = [Pointfront length]-(i+1);
int leng = 3;
if (Index < 0)
{
Leng = 3+index;
index = 0;

}
Nsrange range = {Index,leng};
NSString *STQ = [Pointfront substringwithrange:range];
[Arr ADDOBJECT:STQ];
}
Nsmutablearray *ARR2 = [Nsmutablearray array];
for (int i =[arr count]-1; i>=0; i--) {

[ARR2 Addobject:arr[i]];
}
nsstring*commastring = [[arr2 componentsjoinedbystring:@], "]stringbyappendingstring:pointlast";
if (sign) {
commastring = [sign stringbyappendingstring:commastring];
}
returncommastring;

}

Add a separator of thousand points

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.