//去掉小数点之后的0;
-(NSString*)removeFloatAllZero:(NSString*)string
{
/**
* 第一种方法
*/
//
// NSString * testNumber = string;
// NSString * s = nil;
// NSInteger offset = testNumber.length - 1;
// while (offset)
// {
// s = [testNumber substringWithRange:NSMakeRange(offset, 1)];
// if ([s isEqualToString:@"0"] || [s isEqualToString:@"."])
// {
// offset--;
// }
// else
// {
// break;
// }
// }
// NSString * outNumber = [testNumber substringToIndex:offset+1];
// NSLog(@"%@", outNumber);
// 第二种方法
NSString * testNumber = string;
NSString * outNumber = [NSString
stringWithFormat:@"%@",@(testNumber.floatValue)];
return outNumber;
}
-(NSString*)removeFloatAllZero:(NSString*)string
{
NSString * testNumber = string;
NSString * outNumber = [NSString
stringWithFormat:@"%@",@(testNumber.floatValue)];
// 价格格式化显示
NSNumberFormatter *formatter = [[NSNumberFormatter
alloc]
init];
formatter.numberStyle =
kCFNumberFormatterDecimalStyle;
NSString *formatterString = [formatter
stringFromNumber:[NSNumber
numberWithFloat:[outNumber doubleValue]]];
NSRange range = [formatterString
rangeOfString:@"."];
//现获取要截取的字符串位置
NSLog(@"--------%lu",(unsigned
long)range.length);
if (range.length>0) {
NSString * result = [formatterString
substringFromIndex:range.location];
//截取字符串
if (result.length>=4) {
formatterString=[formatterString
substringToIndex:formatterString.length-1];
}
}
NSLog(@"Formatted number string:%@",formatterString);
NSLog(@"Formatted number string:%@",outNumber);
//
输出结果为:[1223:403] Formatted number string:123,456,789
return formatterString;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。