double类型转字符串去零类方法

2016-03-11  本文已影响223人  然亦伞

//将小数转换为末尾不带零的字符串

+ (NSString *)switchDoubleToNSStringWithoutZero:(double) doubleNumber;

+ (NSString *)switchDoubleToNSStringWithoutZero:(double) doubleNumber{

//如果小数点后面全是0

if (doubleNumber == (NSInteger)doubleNumber) {

return [NSString stringWithFormat:@"%.0f",doubleNumber];

}

//如果是小数

NSString *str = [NSString stringWithFormat:@"%.2f",doubleNumber];

NSInteger index = [str rangeOfString:@"."].location;

if (index) {

NSString *tempStr = [str substringFromIndex:index+1];

if (![tempStr intValue]) {

return [str substringToIndex:index];

}else if(![[tempStr substringFromIndex:1] intValue]){

return [str substringToIndex:index + 2];

}else{

return str;

}

}

return str;

}

上一篇下一篇

猜你喜欢

热点阅读