NSNumberFormatter with comma decimal separator

后端 未结 5 1725
轻奢々
轻奢々 2020-12-03 13:30

I tried to convert an NSString like \"12000.54\" into \"12.000,54\". I wrote an NSNumberFormatter instance.

NSNumberFormatter *formatter = [[[NSNumberFormat         


        
相关标签:
5条回答
  • 2020-12-03 14:17

    I would recommend not hardcoding the separator to ensure the right separator behavior based on the iPhone locale setting. The easiest way to to this is:

    using objective-c

    NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc]init];
    numberFormatter.locale = [NSLocale currentLocale];// this ensures the right separator behavior
    numberFormatter.numberStyle = NSNumberFormatterDecimalStyle;
    numberFormatter.usesGroupingSeparator = YES;
    
    // example for writing the number object into a label
    cell.finalValueLabel.text = [NSString StringWithFormat:@"%@", [numberFormatter stringForObjectValue:numberFromString]]; // your var name is not well chosen
    

    using SWIFT 3

     let formatter = NumberFormatter()
     formatter.locale = NSLocale.current // this ensures the right separator behavior
     formatter.numberStyle = NumberFormatter.Style.decimal
     formatter.usesGroupingSeparator = true
    
     // example for writing the number object into a label
     // your variable "numberFromString" needs to be a NSNumber object
     finalValueLabel.text = formatter.string(from: numberFromString)! // your var name is not well chosen
    

    and I would not use the var-name "numberFromString" because it is an NSNumberFormatter method. Good luck!

    0 讨论(0)
  • 2020-12-03 14:19

    For currency use...

    let numberFormatter = NumberFormatter()
    
    // Produces symbol (i.e. "$ " for en_US_POSIX) and decimal formatting
    numberFormatter.numberStyle = .currency
    
    // Produces commas; i.e. "1,234"
    numberFormatter.usesGroupingSeparator = true
    numberFormatter.groupingSize = 3
    

    Example usage:

    let value: Double = 12345.6789
    numberFormatter.string(from: value as NSNumber)
    

    yields...

    "$ 12,345.68"
    

    as one would expect

    0 讨论(0)
  • 2020-12-03 14:32

    So the question is valid : I'm answering it myself :)

    I have a decimal value read from sqlite (e.g 12000 or 12000.54) directly transformed into NSString. I have to use different separator at some point in my code.

    NSNumberFormatter *formatter = [[[NSNumberFormatter alloc] init] autorelease];
    [formatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
    [formatter setNumberStyle:NSNumberFormatterDecimalStyle];
    [formatter setGroupingSeparator:@""];
    [formatter setDecimalSeparator:@"."];
    
    // Decimal values read from any db are always written with no grouping separator and a comma for decimal.
    
    NSNumber *numberFromString = [formatter numberFromString:@"12000.54"]];
    
    [formatter setGroupingSeparator:@" "]; // Whatever you want here
    [formatter setDecimalSeparator:@","]; // Whatever you want here
    
    NSString *finalValue = [formatter stringFromNumber:numberFromString];
    
    NSLog(@"%@",finalValue); // Print 12 000,54
    

    Problem solved.

    0 讨论(0)
  • 2020-12-03 14:36

    The simplest solution I recently found is:

    NSString *localizedString = [NSString localizedStringWithFormat:@"%@", @1234567];
    

    Also works with ints:

    NSString *localizedString = [NSString localizedStringWithFormat:@"%d", 1234567];
    

    Verified in Xcode 6 playground. But docs say this function has always existed.

    0 讨论(0)
  • 2020-12-03 14:37
    NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
    [numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
    numberFormatter.usesGroupingSeparator = YES;
    [numberFormatter setMaximumFractionDigits:2];
    [numberFormatter setMinimumFractionDigits:2];
    NSString *formattedNumberString = [numberFormatter stringFromNumber:@122344.00];`
    

    This wll fix your rounding up problem if you are dealing with decimal points.

    0 讨论(0)
提交回复
热议问题