2013-04-20 14 views
14

Próbowałem przekonwertować NSString jak "12000.54" na "12.000,54". Napisałem instancję NSNumberFormatter.NSNumberFormatter z przecinkiem dziesiętnym separator

NSNumberFormatter *formatter = [[[NSNumberFormatter alloc] init] autorelease]; 
[formatter setFormatterBehavior:NSNumberFormatterBehavior10_4]; 
[formatter setNumberStyle:NSNumberFormatterDecimalStyle]; 
[formatter setGroupingSeparator:@"."]; 
[formatter setDecimalSeparator:@","]; 

Ale kiedy NSLog to:

NSLog(@"%@",[formatter stringFromNumber:[formatter numberFromString:value]]); 

Drukuje wartość zerową. Jeśli zmienię przecinek z punktem, który działa poprawnie. Chciałbym móc umieścić to, czego chcę dla mojego pola separatorów (przecinek, punkt, ukośnik itp.) I mam różne wyniki: 12/000,54 12.000.54 12.000,54 na przykład.

Czy wiesz, jak sobie z tym poradzić?

+2

Może znalazłem problem . Muszę użyć innego NSFormattera, aby najpierw utworzyć mój numerFromString.Głosowałem za zamknięciem mojego stanowiska – Pierre

+2

Nie zamykaj go, wprowadź odpowiedź sam i sprawdź, inni będą mieli ten sam problem w przyszłości. –

+0

Concur with @DavidH. Pytanie jest poprawne i zawiera kod oraz niezbędne informacje. Naprawdę go przegrywam :-) – LSerni

Odpowiedz

20

Więc pytanie jest ważny: ja odpowiadając na to sam :)

Mam wartość dziesiętną odczytać z SQLite (np 12000 lub 12000.54) bezpośrednio przekształcony NSString. Muszę użyć innego separatora w pewnym momencie w moim kodzie.

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 rozwiązany.

+1

Możesz także użyć właściwości 'doubleValue'' NSString'. – Kevin

37

Nie polecam kodowania na sztywno separatora, aby zapewnić prawidłowe zachowanie separatora w zależności od ustawienia regionalnego iPhone'a. Najprostszym sposobem na to jest:

użyciem 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 

za pomocą 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 

i nie będę używać var-name "numberFromString", ponieważ jest to metoda NSNumberFormatter. Powodzenia!

+0

Super odpowiedź, dzięki – Mutawe

3
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init]; 
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle]; 
numberFormatter.usesGroupingSeparator = YES; 
[numberFormatter setMaximumFractionDigits:2]; 
[numberFormatter setMinimumFractionDigits:2]; 
NSString *formattedNumberString = [numberFormatter stringFromNumber:@122344.00];` 

To rozwiąże problem z zaokrąglaniem, jeśli masz do czynienia z kropkami dziesiętnymi.

6

Najprostszym rozwiązaniem Niedawno znalazłem to:

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

współpracuje także z wskazówki:

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

zweryfikowana w Xcode 6 zabaw. Ale doktorzy mówią, że ta funkcja zawsze istniała.

+0

prosta odpowiedź działa ..! –

0

Do użytku waluty ...

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 

Przykład użycia:

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

plony ...

"$ 12,345.68" 

jak można by oczekiwać

Powiązane problemy