本文介绍了NSNumberFormatter从13000到13K的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用NSNumberFormatter将数字转换为缩写形式.

I would like to convert numbers to the shorter form using NSNumberFormatter.

例如:从23000到23K.

For example: From 23000 to get 23K.

如何使用NSNumberFormatter做到这一点?

我知道可以自己进行计算,但就我而言,这是不正确的方法.

I know it is possible to do calculation on my own, but in my case it is not right way to go.

推荐答案

'K'后缀不被视为标准后缀,因此无法使用NSNumberFormatter来实现.无论如何,这是一种简单的方法.

The 'K' suffix is not recognized as standard, so you can't do this using NSNumberFormatter.anyway this is a simple way to do that.

-(NSString *)kFormatForNumber:(int)number{
    int result;
    if(number >= 1000){ 
        result = number/1000;

        NSString *kValue = [NSString stringWithFormat:@"%dk",result];
        return kValue;
    }
    return [NSString stringWithFormat:@"%d",number];
}

这篇关于NSNumberFormatter从13000到13K的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 03:12