iOS将大数字转换为更小的格式 [英] iOS convert large numbers to smaller format

查看:208
本文介绍了iOS将大数字转换为更小的格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将所有超过3位数字的数字转换为4位数以下的数字?

这正是我的意思:

  10345 = 10.3k 
10012 = 10k
123546 = 123.5k
4384324 = 4.3m

舍入并不是完全重要的,而是一个附加。



我看了一下NSNumberFormatter,但还没有找到合适的解决方案,而我在这里还没有找到合适的解决方案。任何帮助非常感谢,谢谢!

解决方案

这两个方法我已经想出了一起产生所需的效果。这也将自动收起来。这也将通过传递int dec指定总共有多少个数字。

另外,在float to string方法中,可以将 @%。1f更改为 @%。2f @%。3f等等来告诉它显示多少可见小数小数点。

 例如:

52935 ---> 53K
52724 ---> 53.7K




$ b - (NSString *)abbreviateNumber:(int)num withDecimal:(int)dec {

NSString * abbrevNum;
float number =(float)num;

NSArray * abbrev = @ [@K,@M,@B];

(int i = abbrev.count - 1; i> = 0; i--){

//将数组索引转换为1000,1000000 等等
int size = pow(10,(i + 1)* 3);

if(size< = number){
//这里,我们乘以decPlaces,round,然后除以decPlaces。
//这给了我们很好的四舍五入到一个特定的小数位。
number = round(number * dec / size)/ dec;

NSString * numberString = [self floatToString:number];

//添加缩写字母
abbrevNum = [NSString stringWithFormat:@%@%@,numberString,[abbrev objectAtIndex:i]];

NSLog(@%@,abbrevNum);

}

}


return abbrevNum;

$ b - (NSString *)floatToString:(float)val {

NSString * ret = [NSString stringWithFormat:@%。1f,val] ;
unichar c = [ret characterAtIndex:[ret length] - 1];

while(c == 48 || c == 46){// 0 or。
ret = [ret substringToIndex:[ret length] - 1];
c = [ret characterAtIndex:[ret length] - 1];
}

return ret;

$ / code>

希望这可以帮助任何需要它的人!

How can I convert all numbers that are more than 3 digits down to a 4 digit or less number?

This is exactly what I mean:

10345 = 10.3k
10012 = 10k
123546 = 123.5k
4384324 = 4.3m

Rounding is not entirely important, but an added plus.

I have looked into NSNumberFormatter but have not found the proper solution, and I have yet to find a proper solution here on SO. Any help is greatly appreciated, thanks!

解决方案

Here are two methods I have come up with that work together to produce the desired effect. This will also automatically round up. This will also specify how many numbers total will be visible by passing the int dec.

Also, in the float to string method, you can change the @"%.1f" to @"%.2f", @"%.3f", etc to tell it how many visible decimals to show after the decimal point.

For Example:

52935 --->  53K
52724 --->  53.7K





-(NSString *)abbreviateNumber:(int)num withDecimal:(int)dec {

    NSString *abbrevNum;
    float number = (float)num;

    NSArray *abbrev = @[@"K", @"M", @"B"];

    for (int i = abbrev.count - 1; i >= 0; i--) {

        // Convert array index to "1000", "1000000", etc
        int size = pow(10,(i+1)*3);

        if(size <= number) {
            // Here, we multiply by decPlaces, round, and then divide by decPlaces.
            // This gives us nice rounding to a particular decimal place.
            number = round(number*dec/size)/dec;

            NSString *numberString = [self floatToString:number];

            // Add the letter for the abbreviation
            abbrevNum = [NSString stringWithFormat:@"%@%@", numberString, [abbrev objectAtIndex:i]];

            NSLog(@"%@", abbrevNum);

        }

    }


    return abbrevNum;
}

- (NSString *) floatToString:(float) val {

    NSString *ret = [NSString stringWithFormat:@"%.1f", val];
    unichar c = [ret characterAtIndex:[ret length] - 1];

    while (c == 48 || c == 46) { // 0 or .
        ret = [ret substringToIndex:[ret length] - 1];
        c = [ret characterAtIndex:[ret length] - 1];
    }

    return ret;
}

Hope this helps anyone else out who needs it!

这篇关于iOS将大数字转换为更小的格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆