取消小数点后面多余的0

方法一:

NSString * testNumber = @"3.14000000";
NSString * outNumber = [NSString stringWithFormat:@"%@",@(testNumber.floatValue)];
NSLog(@"%@", outNumber);

方法二:

    NSString * testNumber = @"3.1400000";
    NSString * s = nil;
    int 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);
posted @ 2015-08-19 23:15  WillkYang  阅读(390)  评论(0编辑  收藏  举报