Created
November 16, 2015 07:06
-
-
Save czwen/033978056bb3d7b906dc to your computer and use it in GitHub Desktop.
用逗号格式化number
This file contains hidden or 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
- (NSString *)formatNumberDecimal{ | |
if(self){ | |
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init]; | |
formatter.numberStyle = NSNumberFormatterDecimalStyle; | |
[formatter setMaximumFractionDigits:2]; | |
NSString *string = @"0"; | |
if([self multipleLongForNumber]){//long | |
string = [formatter stringFromNumber:[NSNumber numberWithLong:[self longValue]]]; | |
}else{//double | |
string = [formatter stringFromNumber:[NSNumber numberWithDouble:[self doubleValue]]]; | |
} | |
return string; | |
} | |
return @"0"; | |
} | |
- (BOOL)multipleLongForNumber{ | |
NSString *str = [NSString stringWithFormat:@"%@",self]; | |
if([str rangeOfString:@"."].location == NSNotFound){ | |
return YES; | |
} | |
return NO; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment