将 NSString 值(显示为数字)更改为两位小数并计算百分比

Changing NSString value (displayed as number) to two decimal places and make a percentage

当我检索 JSONResponse 时,我检索到一个显示为 0.2434309606330154 的数字。这是我想要的号码,但它不是我想要的格式。它的设置方式是其他三个响应等于 1。

我试过将其转换为 NSNumber,但没有成功。

NSDictionary *parameters = @{
                             @"data": text.text,
                             };

NSMutableString *parameterString = [NSMutableString string];
for (NSString *key in [parameters allKeys]) {
    if ([parameterString length]) {
        [parameterString appendString:@"&"];
    }
    [parameterString appendFormat:@"%@=%@", key, parameters[key]];
}

NSLog(@"A: %@", jsonResponse[@"results"][@"A"]);
ALabel.text = [NSString stringWithFormat:@"%@",jsonResponse[@"results"][@"A"]];
NSString *string = [NSString stringWithFormat:@"%@",jsonResponse[@"results"][@"A"]];
 CGFloat float = [string floatValue];

ALabel.text = [NSString stringWithFormat:@"%.02f",float];

在 Swift 1.2:

self.ALabel?.text = NSString(format: "%.02f%%", string.floatValue) as String
NSString *string = [NSString stringWithFormat:@"%@",jsonResponse[@"results"][@"A"]];
ALabel.text = [NSString stringWithFormat:@"%.02f%%",string.floatValue];

此处输出为 - 0.24%