Skip to content

Instantly share code, notes, and snippets.

@czwen
Created November 16, 2015 07:06
Show Gist options
  • Save czwen/033978056bb3d7b906dc to your computer and use it in GitHub Desktop.
Save czwen/033978056bb3d7b906dc to your computer and use it in GitHub Desktop.
用逗号格式化number
- (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