从NSDecimalNumber中删除否定符号( - )给我一个错误

时间:2015-05-29 04:55:23

标签: ios objective-c nsdecimalnumber

我的array里面有NSDecimalNumbers。我会检查一下这个数字是否为负数。如果是,我想删除负号( - )。这是我的代码:

NSString *amount = [myArray objectAtInde:2]; // Is a NSDecimalNumber with value of: -397.67

    if ([amount doubleValue] < 0) {
        amount = [amount stringByReplacingOccurrencesOfString:@"-" withString:@"- $"];
    }

当我这样做时,我收到以下错误:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSDecimalNumber stringByReplacingOccurrencesOfString:withString:]: unrecognized selector sent to instance 0x7fcaf94eff80'

7 个答案:

答案 0 :(得分:1)

在源文件中添加以下方法

    // multiplies number with -1 if it is less than zero else returns the same 
    - (NSDecimalNumber *)abs:(NSDecimalNumber *)num 
    {
        if ([num compare:[NSDecimalNumber zero]] == NSOrderedAscending) 
        {
            // Number is negative. Multiply by -1
            NSDecimalNumber * negativeOne = [NSDecimalNumber decimalNumberWithMantissa:1
                                                                              exponent:0
                                                                            isNegative:YES];
            return [num decimalNumberByMultiplyingBy:negativeOne];
        } 
        else 
        {
            return num;
        }
    }

并将NSDecimalNumbers的实例转换为abs值而不进行任何类型转换 -

NSDecimalNumbers *number = [myArray objectAtInde:2];

number = [number abs:number];

返回的number将包含abs格式的NSDecimalNumbers值。

答案 1 :(得分:0)

请添加行,

if ([amount doubleValue] < 0) {
    amount=[NSString stringWithFormat:@"%@",[myArray objectAtIndex:2]];//add line
    amount = [amount stringByReplacingOccurrencesOfString:@"-" withString:@""];
}

它可能适合你,

答案 2 :(得分:0)

我刚刚使用了您的代码&amp;它在我的工作正常。

尝试下面的事情,

NSString *amount = @"-397.67";//[myArray objectAtInde:2]; // Is a NSDecimalNumber with value of: -397.67

    if ([amount doubleValue] < 0) {
        amount = [amount stringByReplacingOccurrencesOfString:@"-" withString:@""];
    }

在你的最后尝试。

如果什么都不起作用,那么将负浮动值乘以-1&amp;你会得到正数。如果这是你的要求。

答案 3 :(得分:0)

您的[myArray objectAtInde:2]NSDecimalNumber而非NSString,请尝试以下操作:

NSDecimalNumber *amountNumber = [myArray objectAtInde:2];
NSString *amount = [NSString stringWithFormat:@"%@", amountNumber];

答案 4 :(得分:0)

如果您打算从负面拯救,那么只需将该值转换为绝对值即可。

var user = User as ServiceUser;
var identities = await user.GetIdentitiesAsync();
var googleCredential = identities[0] as Microsoft.WindowsAzure.Mobile.Service.Security.GoogleCredentials;
var accessToken = googleCredential.AccessToken;

值为:397.67

答案 5 :(得分:0)

也许你应该尝试这个:

NSString *amount = @(ABS([myArray[2] doubleValue])).stringValue ; // Is a NSDecimalNumber with value of: -397.67

答案 6 :(得分:0)

试试这个,

NSDecimalNumber *amountNumber = [myArray objectAtIndex:2]; // Is a NSDecimalNumber with value of: -397.67

NSString *stringWithDollarSymbol = [self addDollarSymoblForNumber:amountNumber];

您可以将stringWithDollarSymbol分配给标签

- (NSString *)addDollarSymoblForNumber:(NSDecimalNumber *)decimalNum
{
    NSString *modifiedString = @"";
    if (decimalNum) {
        NSString *numberInStringForm = [decimalNum stringValue];
            if ([decimalNum doubleValue] < 0) { //adds dollor symbol for negative numbers
            modifiedString = [numberInStringForm stringByReplacingOccurrencesOfString:@"-" withString:@"- $"];
        }
        else //adds dollor symbol for positive numbers
        {
            modifiedString = [NSString stringWithFormat:@"$ %@",modifiedString];
        }
    }
    return modifiedString;
}

修改

使用NSNumberFormatter更好的方法:

- (NSString *)formatNumberBasedOnCurrentLocale:(NSDecimalNumber *)number
{
    NSNumberFormatter *formatter = [[NSNumberFormatter  alloc] init];
    [formatter setLocale:[NSLocale currentLocale]];
    [formatter setNumberStyle:NSNumberFormatterCurrencyStyle];
    NSString *result = [formatter stringFromNumber:number];
    if (result.length >0) {
        return result;
    }
    return nil;
}