2010-10-26 13 views
35

this ve that'u okurum. Bunun tam olarak istiyorum:Sıfırlar olmadan bir iki ila iki ondalık basamağı sınırla

1,4324 => "1.43"
9,4000 => "9.4"
43,000 => "43"

9.4 => "9.40" (yanlış)
43,000 = > "43,00" (yanlış)

Her iki soruda da yanıtlar NSNumberFormatter. Yani başarması kolay olmalı, benim için değil.

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 100, 200, 20)]; 

    NSNumberFormatter *doubleValueWithMaxTwoDecimalPlaces = [[NSNumberFormatter alloc] init]; 
    [doubleValueWithMaxTwoDecimalPlaces setNumberStyle:NSNumberFormatterDecimalStyle]; 
    [doubleValueWithMaxTwoDecimalPlaces setPaddingPosition:NSNumberFormatterPadAfterSuffix]; 
    [doubleValueWithMaxTwoDecimalPlaces setFormatWidth:2]; 

    NSNumber *myValue = [NSNumber numberWithDouble:0.]; 
    //NSNumber *myValue = [NSNumber numberWithDouble:0.1]; 

    myLabel.text = [doubleValueWithMaxTwoDecimalPlaces stringFromNumber:myValue]; 

    [self.view addSubview:myLabel]; 
    [myLabel release]; 
    myLabel = nil; 
    [doubleValueWithMaxTwoDecimalPlaces release]; 
    doubleValueWithMaxTwoDecimalPlaces = nil; 
} 

Ben de maksimum iki ondalık basamaklı çift değerlerini biçimlendirmek nasıl Yani

NSString *resultString = [NSString stringWithFormat: @"%.2lf", [myValue doubleValue]]; 
NSLog(@"%@", resultString); 

ile çalıştı? Son konum sıfır içeriyorsa sıfır kalmamalıdır.

Çözüm:

NSNumberFormatter *doubleValueWithMaxTwoDecimalPlaces = [[NSNumberFormatter alloc] init]; 
[doubleValueWithMaxTwoDecimalPlaces setNumberStyle:NSNumberFormatterDecimalStyle]; 
[doubleValueWithMaxTwoDecimalPlaces setMaximumFractionDigits:2]; 
NSNumber *myValue = [NSNumber numberWithDouble:0.]; 
NSLog(@"%@",[doubleValueWithMaxTwoDecimalPlaces stringFromNumber:myValue]]; 
[doubleValueWithMaxTwoDecimalPlaces release]; 
doubleValueWithMaxTwoDecimalPlaces = nil; 
+0

Yuvarlama ister misiniz? Bu 1.4363 => "1.43" veya "1.44" olmalıdır? –

+0

Bunun mantıklı olacağını düşünüyorum. – testing

+0

Tüm bunlardan sonra doubleValueWithMaxTwoDecimalPlaces yayınlamayı unutmayın ... – Lukasz

cevap

41

deneyin sizin biçimlendirici yapılandırırken, aşağıdaki satırı ekleyerek:

[doubleValueWithMaxTwoDecimalPlaces setMaximumFractionDigits:2]; 
0

Nasıl dize ?:

sonundan istenmeyen karakterleri kırparak hakkında
NSString* CWDoubleToStringWithMax2Decimals(double d) { 
    NSString* s = [NSString stringWithFormat:@"%.2f", d]; 
    NSCharacterSet* cs = [NSCharacterSet characterSetWithCharacterInString:@"0."]; 
    NSRange r = [s rangeOfCharacterInSet:cs 
           options:NSBackwardsSearch | NSAnchoredSearch]; 
    if (r.location != NSNotFound) { 
     s = [s substringToIndex:r.location]; 
    } 
    return s; 
} 
+1

Bu çözüm işe yarasa da çok daha iyi bir çözüm var (bkz. Kabul edilen yanıt) – Muxa

0
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init]; 
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle]; 
[numberFormatter setRoundingMode:NSNumberFormatterRoundDown]; 
[numberFormatter setMinimumFractionDigits:2]; 
numberFormatter.positiveFormat = @"0.##"; 
NSNumber *num = @(total_Value); 
+0

cevabınızı açıklayın –

+0

@SahilMittal Yukarıdaki kod NSNumberFormatterRoundDown dönüş değeri yuvarlak olmadan, daha sonra pozitifFormat sadece iki kesir nokta değeri verir. –

İlgili konular