如何在iPhone应用程序中获取带有两个小数位的String [英] how to get String with two decimal places in iphone app

查看:112
本文介绍了如何在iPhone应用程序中获取带有两个小数位的String的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有应用程序,其中我有方法在NSString中插入逗号数千个值我也想包括decimall如果我现在输入1000然后它显示1,000使用此方法但我想这样的1,000.00这是我的方法任何想法如何解决这个问题。

I have app in which i have method which inserts comma in NSString in thousands values i also want to include decimall like if i enter now 1000 then it shows 1,000 using this method but i want 1,000.00 like this here is my method any idea how to fix this issue.

- (NSString *)formatStringF:(NSString *)string {
// Strip out the commas that may already be here:
NSString *newString = [string stringByReplacingOccurrencesOfString:@"," withString:@""];
if ([newString length] == 0) {
    return nil;
}

// Check for illegal characters
NSCharacterSet *disallowedCharacters = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789."] invertedSet];
NSRange charRange = [newString rangeOfCharacterFromSet:disallowedCharacters];
if ( charRange.location != NSNotFound) {
    return nil;
}

// Split the string into the integer and decimal portions
NSArray *numberArray = [newString componentsSeparatedByString:@"."];
if ([numberArray count] > 2) {
    // There is more than one decimal point
    return nil;
}

// Get the integer
NSString *integer           = [numberArray objectAtIndex:0];
NSUInteger integerDigits    = [integer length];
if (integerDigits == 0) {
    return nil;
}


// Format the integer.
// You can do this by first converting to a number and then back to a string,
// but I would rather keep it as a string instead of doing the double conversion.
// If performance is critical, I would convert this to a C string to do the formatting.
NSMutableString *formattedString = [[NSMutableString alloc] init];
if (integerDigits < 4) {
    [formattedString appendString:integer];
} else {
    // integer is 4 or more digits
    NSUInteger startingDigits = integerDigits % 3;
    if (startingDigits == 0) {
        startingDigits = 3;
    }
    [formattedString setString:[integer substringToIndex:startingDigits]];
    for (NSUInteger index = startingDigits; index < integerDigits; index = index + 3) {
        [formattedString appendFormat:@",%@", [integer substringWithRange:NSMakeRange(index, 3)]];
    }
}

// Add the decimal portion if there
if ([numberArray count] == 2) {
    [formattedString appendString:@"."];
    NSString *decimal = [numberArray objectAtIndex:1];
    if ([decimal length] > 0) {
        [formattedString appendString:decimal];
    }
}






return formattedString;
 }


推荐答案

为什么重新发明轮子?只需使用 NSNumberFormatter

Why reinvent the wheel? Just use an NSNumberFormatter.

double someNumber = 1000.0;

NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
// Use decimal style - this includes grouping
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
// Force two decimal places
[formatter setMinimumFractionDigits:2];
[formatter setMsximumFractionDigits:2];

NSString *formattedNumber = [formatter stringFromNumber:@(someNumber)];

这也有一个优点,就是它会根据用户的语言环境正确格式化数字。

This also has the advantage that it will format the number properly based on the user's locale.

它将显示为1,000.00或1.000,00或1 000,00或其他一些适当的用户格式。

It will appears as 1,000.00 or 1.000,00 or 1 000,00 or some other proper format for the user.

这篇关于如何在iPhone应用程序中获取带有两个小数位的String的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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