IOS String Thousand Division

Source: Internet
Author: User

/**
* Formatted string 3-bit group (bitwise trade-offs, no-entry)
*
* @param numberobject string to be processed (can be nsnumber)
* Number of bits reserved by @param bits
*
* @return Return string
*/
+ (NSString *) Numberdivision: (ID) numberobject bits: (Nsinteger) bits;
/**
* Formatted string 3-bit group (bitwise trade-offs, no-entry)
*
* @param numberobject string to be processed (can be nsnumber)
* Number of bits reserved by @param bits
* @param whether the isinteger automatically retains integers (for example: Yes 1.00 is 1)
* @return Return string
*/
+ (NSString *) Numberdivision: (ID) numberobject bits: (Nsinteger) bits Isinteger: (BOOL) Isinteger;

+ (NSString *) Numberdivision: (ID) numberobject bits: (Nsinteger) Bits {

NSString * Number = [NSString stringwithformat:@ "%@", Numberobject];
if ([NSString Isblankstring:number]) {
NSLog (@ "string is empty, should be noted");
}
if ([number containsstring:@]. ") {This method iOS8 above is available
if ([number rangeofstring:@]. Location! = Nsnotfound) {
Nsarray *arrnumber = [[Nsarray alloc]initwitharray:[number componentsseparatedbystring:@].
nsmutablestring *arrint = [[nsmutablestring alloc]initwithstring:[arrnumber Firstobject]];
int index = 0;
for (int i = 1; I <= arrint.length; i + +) {
if (i% 3 = = 0 && arrint.length > (i + index)) {
[Arrint insertstring:@], "atIndex:arrInt.length-(i + index)];
Index + +;
}
}
if (bits! = 0) {
NSString *decimal = Arrnumber.lastobject;
if (decimal.length >= bits) {
decimal = [decimal substringtoindex:bits];
}else {
while (Decimal.length < bits) {
decimal = [Decimal stringbyappendingstring:@ "0"];
}
}
return [NSString stringwithformat:@ "%@.%@", Arrint,decimal];
}else {
return [NSString stringwithformat:@ "%@", Arrint];
}

} else {
nsmutablestring *arrint = [[nsmutablestring alloc]initwithstring:number];
int index = 0;
for (int i = 1; I <= arrint.length; i + +) {
if (i% 3 = = 0 && arrint.length > (i + index)) {
[Arrint insertstring:@], "atIndex:arrInt.length-(i + index)];
Index + +;
}
}
if (bits! = 0) {
NSString *decimal = [NSString stringwithformat:@ "%@", @ "0"];
while (Decimal.length < bits) {
decimal = [Decimal stringbyappendingstring:@ "0"];
}
return [NSString stringwithformat:@ "%@.%@", Arrint,decimal];
}else {
return [NSString stringwithformat:@ "%@", Arrint];
}
}
}

+ (NSString *) Numberdivision: (ID) numberobject bits: (Nsinteger) bits Isinteger: (BOOL) Isinteger {
NSString *number = [NSString numberdivision:numberobject bits:bits];
if ([number rangeofstring:@]. Location! = Nsnotfound) {
Nsarray *numberarray = [number componentsseparatedbystring:@ "];
NSString *decimal = Numberarray.lastobject;
if (Decimal.length >1) {
if (Decimal.floatvalue = = 0) {
return numberarray.firstobject;
}
NSString *tempstring = nil;
Nsinteger offset = decimal.length-1;
while (offset) {
tempstring = [Decimal substringwithrange:nsmakerange (offset, 1)];
if ([tempstring isequaltostring:@ "0"]) {
Offset--;
}else {
Break
}
decimal = [decimal substringtoindex:offset];
return [NSString stringwithformat:@ "%@.%@", Numberarray.firstobject,decimal];
}
}else {
if ([Decimal isequaltostring:@ "0"]) {
return numberarray.firstobject;
}else {
return number;
}
}

}else {
return number;
}
return number;
}

IOS String Thousand Division

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.