美文网首页
NSString 没3位分割 ,

NSString 没3位分割 ,

作者: 林希品 | 来源:发表于2022-09-19 10:25 被阅读0次

    方法

    eg.数字以逗号隔开 例:123,321.11:

    @interface NSString (NumberSplit)
    
    /**
     *数字以逗号隔开 例:123,321.11
     */
    - (NSString *)xp_numberSplitWithComma;
    /**
     *@param puctutation 符合
     *数字以某个符合隔开 例:123,321.11
     */
    - (NSString *)xp_numberSplitWithPunctutaion:(NSString *)puctutation;
    /**
     *XX.XX万     XX.XX亿
     */
    - (NSString *)subscribeCountUnit;
    @end
    
    
    @implementation NSString (NumberSplit)
    
    - (NSString *)xp_numberSplitWithComma{
        if ([self private_isNumber]) {
            NSMutableString *muString = [NSMutableString stringWithString:self];
            return [self private_insert:muString withPunctuation:@","];
        }else{
            return self;
        }
    }
    - (NSString *)xp_numberSplitWithPunctutaion:(NSString *)puctutation{
        if ([self private_isNumber]) {
             NSMutableString *muString = [NSMutableString stringWithString:self];
             return [self private_insert:muString withPunctuation:puctutation];
         }else{
             return self;
         }
    }
    - (NSMutableString *)private_insert:(NSMutableString *)string withPunctuation:(NSString *)punctuation{
        NSUInteger maxLength = string.length;
        if ([string containsString:punctuation]) {
            maxLength = [string rangeOfString:punctuation].location;
        }else if ([string containsString:@"."]){
            maxLength = [string rangeOfString:@"."].location;
        }
        if (maxLength-([string containsString:@"-"]?1:0)>3) {
            [string insertString:punctuation atIndex:(maxLength-3)];
            [self private_insert:string withPunctuation:punctuation];
        }else{
            return string;
        }
        return string;
    }
     - (Boolean)private_isNumber{
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",@"^[-0-9.]*$"];
        if ([predicate evaluateWithObject:self]) {
            return YES;
         }
        return NO;
     }
    
    - (NSString *)subscribeCountUnit {
        NSInteger count = [self integerValue];
        if (count < 10000) {
            return [NSString stringWithFormat:@"%ld",count];
        }else if (count < 100000000 && count >= 10000) {
            NSInteger tenThouand = count/10000;
            NSInteger thousand = count%10000/1000;
    //        return [NSString stringWithFormat:@"%ld.%ld万",tenThouand,thousand>=5?thousand+1:thousand];//进位
            return [NSString stringWithFormat:@"%ld.%ld万",tenThouand,thousand];
        } else{
            NSInteger yi = count/100000000;
            NSInteger tenThouand = count%100000000/10000000;
    //        return [NSString stringWithFormat:@"%ld.%ld万",tenThouand,thousand>=5?thousand+1:thousand];//进位
            return [NSString stringWithFormat:@"%ld.%ld亿",yi,tenThouand];
        }
        return @"0";
    }
    
    
    

    相关文章

      网友评论

          本文标题:NSString 没3位分割 ,

          本文链接:https://www.haomeiwen.com/subject/zuorortx.html