0

float 値の計算のバグについて知っている人はいますか (私の場合、テキストから float に変換された通貨値)。シミュレーターでテストしましたが、計算された値はすべて正しいです。次に、iPhoneデバイス自体でテストすると、すべての値が0.59以上で表示されます。iPad デバイスでは、すべての値でわずか 0.15 増加しています。

これが私のコードです

- (void)viewDidLoad
{
    [super viewDidLoad];

// current account balance from Coredata Dataset
if (!self.FSDataSet.konto || [self.FSDataSet.konto isEqualToString:@""]) {
    self.txtKontostand.text = @"Nicht möglich!";
} else {
    // fetch request for current changes of currency values
    NSFetchRequest *fetchRequestFahrschueler = [NSFetchRequest fetchRequestWithEntityName:NSStringFromClass([FSKontoUnterTab class])];
    NSPredicate *predicateKundenNr = [NSPredicate predicateWithFormat:@"kundennr == %@", self.FSDataSet.kundennr];
    fetchRequestFahrschueler.predicate = predicateKundenNr;

    // put datasets in array
    NSArray *ergebnisFahrschueler= [[[CoreDataHelper sharedInstance] managedObjectContext] executeFetchRequest:fetchRequestFahrschueler error:nil];

    // variables to calculate the new account balance
    CGFloat soll;
    CGFloat haben;
    CGFloat konto;

    // account balance string converting to a flot value
    // comma replace with point
    NSString *kontostand = [self.FSDataSet.konto stringByReplacingOccurrencesOfString:@"," withString:@"."];
    CGFloat testKonto = kontostand.floatValue;

    // NSArray Iterator workaround
    // loop array and assign current dataset from entity (the managed object class)

    for (NSInteger i = 0; i < [ergebnisFahrschueler count]; i++) {
        FSKontoUnterTab *myset = [ergebnisFahrschueler objectAtIndex:i];


        CGFloat gesamtpreis = [myset.preis stringByReplacingOccurrencesOfString:@"," withString:@"."].floatValue;

        // positive currency values
        CGFloat aktHaben = [myset.bar stringByReplacingOccurrencesOfString:@"," withString:@"."].floatValue;
// calculate

        soll = soll + gesamtpreis;
        haben = haben +aktHaben;


    }
    //konto = soll-haben;
    konto = (testKonto - soll)+haben;

    // convert back to string and display in textfield
    self.txtKontostand.text = [[NSString stringWithFormat:@"%.2f", konto] stringByReplacingOccurrencesOfString:@"." withString:@","];
}
4

0 に答える 0