有时候需要显示数字,但是小数点后面如果是0就不显示
+(NSString *)stringFromDouble:(CGFloat)value{
if (fmodf(value, 1)==0) {//如果有一位小数点
return [NSString stringWithFormat:@"%.0f",value];
} else if (fmodf(value*10, 1)==0) {//如果有两位小数点
return [NSString stringWithFormat:@"%.1f",value];
} else {
return [NSString stringWithFormat:@"%.2f",value];
}
}
显示人民币价格
+(NSString *)numberFormatterWith:(CGFloat)number{
NSNumber *currentNumber=[NSNumber numberWithDouble:number];
NSNumberFormatter *formatter=[[NSNumberFormatter alloc] init];
[formatter setPositiveFormat:@"###,###.00"];
formatter.positivePrefix=@"¥";
// formatter.positiveSuffix=@"元";
return [formatter stringFromNumber:currentNumber];
}
显示不带小数点的格式化数字
+(NSString *)numberFormatterWithInt:(NSInteger)number{
NSNumber *currentNumber=[NSNumber numberWithDouble:number];
NSNumberFormatter *formatter=[[NSNumberFormatter alloc] init];
[formatter setPositiveFormat:@"###,###"];
return [formatter stringFromNumber:currentNumber];
}