Created
March 14, 2012 17:27
-
-
Save tanapon/2038060 to your computer and use it in GitHub Desktop.
Adding Thousands Separator commas to a number manually without using NSNumber formatting method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// NSStringCategory.h | |
// AdamoCalc | |
// | |
// Created by Tanapon Petapanpiboon on 10/11/11. | |
// Copyright 2011 toptanapon@gmail.com. All rights reserved. | |
// | |
#import <Foundation/Foundation.h> | |
@interface NSString(NumberMethods) | |
-(NSString *)stringWithThousandsSeparator; | |
-(NSString *)numberStringWithFormat; | |
-(NSString *)numberStringByRemovingFormat; | |
-(int)digitCount; | |
-(NSString *)numberStringByRemovingTrailingZeros; | |
-(NSString *)numberStringWithLimitedDigits; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// NSStringCategory.m | |
// AdamoCalc | |
// | |
// Created by Tanapon Petapanpiboon on 10/11/11. | |
// Copyright 2011 toptanapon@gmail.com. All rights reserved. | |
// | |
#import "NSStringCategory.h" | |
@implementation NSString(NumberMethods) | |
//This is a simple version for adding a thousands separator comma | |
-(NSString *)stringWithThousandsSeparator | |
{ | |
int l = [self length]; | |
int a = l/3; | |
int b = l%3; | |
int x = (((a-1)*3)+b); | |
NSMutableString *mutableString = [self mutableCopy]; | |
if (x>0) { | |
x=x+3; | |
do { | |
x=x-3; | |
[mutableString insertString:@"," atIndex:x]; | |
//NSLog(@"%i",x); | |
} while (x>3); | |
} | |
self = [NSString stringWithString: mutableString]; | |
[mutableString release]; | |
return self; | |
} | |
//Removes , | |
-(NSString *)numberStringByRemovingFormat | |
{ | |
return [self stringByReplacingOccurrencesOfString:@"," withString:@""]; | |
} | |
//This one is used in Adamo Calculator | |
//Supports 1000, -1000, 1000.01, -1000.01 | |
-(NSString *)numberStringWithFormat | |
{ | |
if (![self isEqualToString:@"Error"]) { | |
NSMutableString *beforeString = nil; | |
NSString *afterString = nil; | |
BOOL isNegative = [self rangeOfString:@"-"].location != NSNotFound; | |
NSLog(@"Before:%@",self); | |
if (isNegative) { | |
self = [self stringByReplacingOccurrencesOfString:@"-" withString:@""]; | |
NSLog(@"After:%@",self); | |
} | |
if ([self rangeOfString:@"."].location == NSNotFound) { | |
beforeString = [self mutableCopy]; | |
} else { | |
NSArray *stringComponents = [self componentsSeparatedByString:@"."]; | |
beforeString = [[stringComponents objectAtIndex:0] mutableCopy]; | |
afterString = [stringComponents objectAtIndex:1]; | |
} | |
NSLog(@"beforeString:%@",beforeString); | |
int l = [beforeString length]; | |
int a = l/3; | |
int b = l%3; | |
int x = (((a-1)*3)+b); | |
if (x>0) { | |
x=x+3; | |
do { | |
x=x-3; | |
[beforeString insertString:@"," atIndex:x]; | |
//NSLog(@"%i",x); | |
} while (x>3); | |
} | |
if (afterString) { | |
self = [NSString stringWithFormat:@"%@%@%@",beforeString,@".",afterString]; | |
} else { | |
self = [NSString stringWithString:beforeString]; | |
} | |
//NSLog(@"%@",self); | |
if (isNegative) { | |
self = [NSString stringWithFormat:@"-%@",self]; | |
} | |
[beforeString release]; | |
} | |
return self; | |
} | |
//Counts digits without . or - | |
-(int)digitCount | |
{ | |
return [[[[self stringByReplacingOccurrencesOfString:@"," withString:@""] stringByReplacingOccurrencesOfString:@"." withString:@""] stringByReplacingOccurrencesOfString:@"-" withString:@""] length]; | |
} | |
//Removes decimal trailing zeros | |
-(NSString *)numberStringByRemovingTrailingZeros | |
{ | |
BOOL isDecimal = [self rangeOfString:@"."].location != NSNotFound; | |
if (isDecimal) { | |
int index = (int)[self length] - 1; | |
BOOL trim = NO; | |
while (([self characterAtIndex:index] == 48 || [self characterAtIndex:index] == 46) && index>0) { // (0 or .) | |
//If the last character is a dot @".", exit function afterwards | |
if ([self characterAtIndex:index] == 46) { | |
index--; | |
trim = YES; | |
break; | |
} | |
index--; | |
trim = YES; | |
} | |
if (trim) { | |
self = [self substringToIndex:index + 1]; | |
} | |
} | |
return self; | |
} | |
//The number string passed to this method, must not have commas. Call numberStringByRemovingFormat first. | |
-(NSString *)numberStringWithLimitedDigits | |
{ | |
//If int value is more than 999,999,999, or if the string is "nan" (by 0 / 0) | |
if ([self intValue] > kMaximumAmountAllowed || [self intValue] < kMaximumAmountAllowed*-1 || [self isEqualToString:@"nan"] || [self isEqualToString:@"inf"]) { | |
self = @"Error"; | |
} else { | |
int length = (int)[self length]; | |
int maximumDigits = kMaximumDigitsAllowed; | |
int index = length - 1; | |
BOOL trim = NO; | |
//If a . (dot) is found, index++ | |
if ([self rangeOfString:@"."].location != NSNotFound) { | |
index++; | |
} | |
//If a - (negative) is found, maximumDigits++ | |
if ([self rangeOfString:@"-"].location != NSNotFound) { | |
maximumDigits++; | |
} | |
//as long as length is over maximum digits allowed | |
while (length > maximumDigits) { | |
index--; | |
length--; | |
//if the last char is a (.), index-- | |
if ([self characterAtIndex:index] == 46) { | |
index--; | |
} | |
trim = YES; | |
} | |
if (trim) { | |
self = [self substringToIndex:index +1]; | |
} | |
} | |
return self; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment