从 NSDecimalNumber 中删除负号 (-) 给我一个错误

Remove negative symbol (-) from NSDecimalNumber gives me an error

我有一个 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'

请加行,

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

它可能适合你,

我刚刚使用了您的代码,它在我这边运行良好。

试过下面的东西,

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

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

最后试试吧。

如果没有任何效果,则将负浮点值乘以 -1 & 你将得到正数。如果这是你的要求。

你的 [myArray objectAtInde:2]NSDecimalNumber 而不是 NSString,试试这个:

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

在您的源文件中添加以下方法

    // 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 将包含 NSDecimalNumbers 格式的 abs 值。

如果你的目的是从负值中拯救,那么只需将值转换为绝对值即可。

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

NSLog(@"The value is : %@",[NSString stringWithFormat:@"%.2f", fabs([amount doubleValue])]);

值为:397.67

也许你应该试试这个:

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

试试这个,

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;
}