将int转换为缩短的格式化字符串 [英] Convert int to shortened, formatted string

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

问题描述

用户将以 int 输入美元值,我想将结果转换为缩短的格式化字符串。因此,如果用户输入1700,字符串将会说$ 1.7k。如果用户输入32600000,字符串将显示$ 32.6m。



更新



这里是我到目前为止的代码。它似乎工作在数字〜10k。我只是添加更多if语句为更大的数字。但是是否有更有效的方法来做到这一点?

  NSNumberFormatter * nformat = [[NSNumberFormatter alloc] init]; 
[nformat setFormatterBehavior:NSNumberFormatterBehavior10_4];
[nformat setCurrencySymbol:@$];
[nformat setNumberStyle:NSNumberFormatterCurrencyStyle];
double doubleValue = 10200;
NSString * stringValue = nil;
NSArray * abbrevations = [NSArray arrayWithObjects:@k,@m,@b,@t,nil];

(缩写中的NSString * s)
{

doubleValue / = 1000.0;

if(doubleValue< 1000.0)
{

if((long long)doubleValue%(long long)100 == 0){
[nformat setMaximumFractionDigits:0];
} else {
[nformat setMaximumFractionDigits:2];
}

stringValue = [NSString stringWithFormat:@%@,[nformat stringFromNumber:[NSNumber numberWithDouble:doubleValue]]]
NSUInteger stringLen = [stringValue length];

if([stringValue hasSuffix:@。00])
{
//删除后缀
stringValue = [stringValue substringWithRange:NSMakeRange(0,stringLen- 3)];
} else if([stringValue hasSuffix:@。0]){

//删除后缀
stringValue = [stringValue substringWithRange:NSMakeRange(0,stringLen- ];

} else if([stringValue hasSuffix:@0]){

//删除后缀
stringValue = [stringValue substringWithRange:NSMakeRange(0,stringLen -1)];
}


//在其末尾添加字母后缀
stringValue = [stringValue stringByAppendingString:s];

// stringValue = [NSString stringWithFormat:@%@%@,[nformat stringFromNumber:[NSNumber numberWithDouble:doubleValue]],s];
break;
}
}

NSLog(@Cash =%@,stringValue);


解决方案

长值= 1700llu; 
// value = 32600001llu;
// value = UINT64_MAX;

NSUInteger index = 0;
double dvalue =(double)value;
//更新为使用正确的SI符号(http://en.wikipedia.org/wiki/SI_prefix)
NSArray * suffix = @ [@,@k,@M ,G,T,P,E];

while((value / = 1000)&& ++ index)dvalue / = 1000;

NSString * svalue = [NSString stringWithFormat:@$%。* f%@,
//使用boolean作为0或1作为精度
(int) < 100.0&&((unsigned)((dvalue-(unsigned)dvalue)* 10)> 0)),
dvalue,[suffix objectAtIndex:index]
NSLog(@Value:%@,svalue);






ARC本地化版本

  unsigned long long value = 1700llu; 
// value = 32600001llu;
// value = UINT64_MAX;

NSUInteger index = 0;
double dvalue =(double)value;
//更新为使用正确的SI符号(http://en.wikipedia.org/wiki/SI_prefix)
NSArray * suffix = @ [@,@k,@M ,G,T,P,E];

while((value / = 1000)&& ++ index)dvalue / = 1000;

NSNumberFormatter * formatter = [[NSNumberFormatter alloc] init];
//德国示例
[formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@de-de]];
//将小数位设置为0或1
[formatter setMaximumFractionDigits:(int)(dvalue< 100.0&&((unsigned)((dvalue - (unsigned)dvalue)* 10)> ; 0))];

NSString * svalue = [[formatter stringFromNumber:[NSNumber numberWithFloat:dvalue]]
stringByAppendingString:[suffix objectAtIndex:index]];

NSLog(@Value:%@,svalue);


The user will enter a dollar value as an int, and I'd like to convert the result into a shortened, formatted string. So if the user enters 1700, the string would say "$1.7k". If the user enters 32600000, the string would say "$32.6m".

Update

Here's the code I have so far. It seems to be working for numbers ~10k. I would just add more if statements for bigger numbers. But is there a more efficient way to do this?

NSNumberFormatter *nformat = [[NSNumberFormatter alloc] init]; 
[nformat setFormatterBehavior:NSNumberFormatterBehavior10_4]; 
[nformat setCurrencySymbol:@"$"]; 
[nformat setNumberStyle:NSNumberFormatterCurrencyStyle]; 
double doubleValue = 10200; 
NSString *stringValue = nil; 
NSArray *abbrevations = [NSArray arrayWithObjects:@"k", @"m", @"b", @"t", nil] ; 

for (NSString *s in abbrevations) 
{ 

    doubleValue /= 1000.0 ; 

    if ( doubleValue < 1000.0 ) 
    { 

        if ( (long long)doubleValue % (long long) 100 == 0 ) { 
            [nformat setMaximumFractionDigits:0]; 
        } else {                 
            [nformat setMaximumFractionDigits:2]; 
        } 

        stringValue = [NSString stringWithFormat: @"%@", [nformat stringFromNumber: [NSNumber numberWithDouble: doubleValue]] ]; 
        NSUInteger stringLen = [stringValue length]; 

        if ( [stringValue hasSuffix:@".00"] ) 
        {                
            // Remove suffix 
            stringValue = [stringValue substringWithRange: NSMakeRange(0, stringLen-3)];             
        } else if ( [stringValue hasSuffix:@".0"] ) { 

            // Remove suffix 
            stringValue = [stringValue substringWithRange: NSMakeRange(0, stringLen-2)]; 

        } else if ( [stringValue hasSuffix:@"0"] ) { 

            // Remove suffix 
            stringValue = [stringValue substringWithRange: NSMakeRange(0, stringLen-1)];         
        } 


        // Add the letter suffix at the end of it 
        stringValue = [stringValue stringByAppendingString: s]; 

        //stringValue = [NSString stringWithFormat: @"%@%@", [nformat stringFromNumber: [NSNumber numberWithDouble: doubleValue]]  , s] ; 
        break ; 
    }    
}  

NSLog(@"Cash = %@", stringValue); 

解决方案

unsigned long long value = 1700llu;
//value = 32600001llu;
//value = UINT64_MAX;

NSUInteger index = 0;
double dvalue = (double)value;
//Updated to use correct SI Symbol ( http://en.wikipedia.org/wiki/SI_prefix )
NSArray *suffix = @[ @"", @"k", @"M", @"G", @"T", @"P", @"E" ];

while ((value/=1000) && ++index) dvalue /= 1000;

NSString *svalue = [NSString stringWithFormat:@"$%.*f%@",
                    //Use boolean as 0 or 1 for precision
                    (int)(dvalue < 100.0 && ((unsigned)((dvalue - (unsigned)dvalue) * 10) > 0)),
                    dvalue, [suffix objectAtIndex:index]];
NSLog(@"Value: %@", svalue);


ARC Localized Version

unsigned long long value = 1700llu;
//value = 32600001llu;
//value = UINT64_MAX;

NSUInteger index = 0;
double dvalue = (double)value;
//Updated to use correct SI Symbol ( http://en.wikipedia.org/wiki/SI_prefix )
NSArray *suffix = @[ @"", @"k", @"M", @"G", @"T", @"P", @"E" ];

while ((value/=1000) && ++index) dvalue /= 1000;

NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
//Germany Example
[formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"de-de"]];
//Set fractional digits to 0 or 1
[formatter setMaximumFractionDigits:(int)(dvalue < 100.0 && ((unsigned)((dvalue - (unsigned)dvalue) * 10) > 0))];

NSString *svalue = [[formatter stringFromNumber:[NSNumber numberWithFloat:dvalue]]
                    stringByAppendingString:[suffix objectAtIndex:index]];

NSLog(@"Value: %@", svalue);

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

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