我想将数字13456显示为13.456K,3456789显示为34.56L。在IOS中是否有任何格式化程序转换为K(千),L(Lakh),M(百万),C(Crore)后缀?< / p>
答案 0 :(得分:4)
iOS
中没有库或sdk缩短或转换货币使用缩写,如 K(千), M(百万)类似 L(10万), C(1000万)等,因为这些缩写在不同国家和地区有所不同。
您必须根据您的号码所属的类别转换该号码,并使用NSNumberFormatter
NSNumberFormatterCurrencyStyle
您可以根据您的要求格式化您的号码。
您可以使用以下代码根据您的要求转换货币金额:
float shortenedAmount = actualAmount;
NSString *suffix = @"";
if(currency >= 10000000.0f) {
suffix = @"C";
shortenedAmount /= 10000000.0f;
}
else if(currency >= 1000000.0f) {
suffix = @"M";
shortenedAmount /= 1000000.0f;
}
else if(currency >= 100000.0f) {
suffix = @"L";
shortenedAmount /= 100000.0f;
}
else if(currency >= 1000.0f) {
suffix = @"K";
shortenedAmount /= 1000.0f;
}
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle: NSNumberFormatterCurrencyStyle];
NSString *numberAsString = [numberFormatter stringFromNumber:[NSNumber numberWithFloat:shortenedAmount]];
NSString *requiredString = [NSString stringWithFormat:@"%@%@", numberAsString, suffix];
答案 1 :(得分:0)
答案 2 :(得分:0)
请尝试以下自定义方法 -
-(NSString *)stringFormating:(int)numValue
{
NSString *valueString = [NSString stringWithFormat:@"%d", numValue];
if ([valueString length]>7)
{
NSString *strsecondPart= [valueString substringFromIndex:[valueString length] -7];
return [NSString stringWithFormat:@"%@.%dC",[valueString substringToIndex:[valueString length] -7],[strsecondPart intValue]];
}
else if ([valueString length]>5)
{
NSString *strsecondPart= [valueString substringFromIndex:[valueString length] -5];
return [NSString stringWithFormat:@"%@.%dL",[valueString substringToIndex:[valueString length] -5],[strsecondPart intValue]];
}
else if ([valueString length]>3)
{
NSString *strsecondPart= [valueString substringFromIndex:[valueString length] -3];
return [NSString stringWithFormat:@"%@.%dK",[valueString substringToIndex:[valueString length] -3],[strsecondPart intValue]];
}
return valueString;
}
NSLog(@"%@", [self stringFormating:100000]);
输出为1.0L
NSLog(@"%@", [self stringFormating:123456]);
输出为1.23456L
答案 3 :(得分:0)
这是 Swift 的实现: 在 Swift 中将 NumberFormatter (NSNumberFormatter) 转换为印度编号格式的货币。
extension Int {
func withCommas() -> String {
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .decimal
numberFormatter.locale = Locale(identifier: "en_IN") // English (Indian) locale with english language is used
return numberFormatter.string(from: NSNumber(value:self))!
}
}
//Easy to use:--
let strValue = "64567890"
let intValue = (strValue! as NSString).integerValue
print(intValue.withCommas()) // 6,45,67,890
// cell.lbl.text = intValue.withCommas()