Change NSNumberFormatter's negative format from (xxx.xx) to -xxx.xx

前端 未结 4 1942
栀梦
栀梦 2021-01-02 14:25

I want to change my NSNumberformatter from displaying negative numbers with parenthesis around them to putting the minus sign in front (or whatever the localized standard is

4条回答
  •  梦毁少年i
    2021-01-02 15:01

    All of the answers given assume that the currency is to two decimal places and also uses commas as thousand separators. Obviously there are a number of currencies that don't conform to this standard, http://en.wikipedia.org/wiki/Decimal_mark, so I use the following technique

        NSString * formattedBalance = [currencyFormatter stringFromNumber:balance];
        if([formattedBalance rangeOfString:@"("].location != NSNotFound ) {
            formattedBalance = [NSString stringWithFormat:@"-%@",[[formattedBalance stringByReplacingOccurrencesOfString:@")" withString:@"" ]stringByReplacingOccurrencesOfString:@"(" withString:@"" ]];
        }
    

提交回复
热议问题