0

有谁知道计算浮点值的错误(在我的情况下,货币值从文本转换为浮点数)。我一直在模拟器上测试它,所有计算值都是正确的。然后在 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