Loosing precision of a number somewhere

Hi


I am loosing precision of a number somewhere in my code, the number is a price in a pricelist I have in a DB, the workflow is as follows


The App requests the price from a webservice that responds with a JSON object, when I dump the JSON object in my app using NSLog I can see the precision being correct 6.95 in this case


I then use this value and insert it in a Local SQLite DB using the following command


NSString *query = [NSString stringWithFormat:@"insert into prl (currencyName, price, priceIncTAX, validFrom, validTo, itemId) values('%@', %d, %d, '%@', '%@', '%@')", currencyName, [price intValue], [priceIncTAX intValue], validFrom, validTo, itemId];


If I look at the price variable using NSLog like below I can see the correct precision of 6.95


NSLog(@"Price for this item = %@", price);


The price variable comes from the JSON ibject like below


NSString *price = [priceListObject objectForKey:@"Price"];


So far everything looks ok, every NSLog output shows correctly 6.95


Now I try and select from the DB as follows


/
    NSString *query = [NSString stringWithFormat:@"select tilldata.itemId, tilldata.TradeItemDesc, prl.price from tilldata JOIN prl ON tilldata.itemId = prl.itemId where noos14 like '%%%@%%'", barCode];
    NSLog(@"Query looks like:%@", query);
   
    /
    NSArray *items = [[NSArray alloc] initWithArray:[self.dbManager loadDataFromDB:query]];
    NSLog(@"Items array holds: %@", items);


Now all of a sudden the items array has the price as 6 and not 6.95


I am assuming I am using NSString incorrectly somehow in all of this but I cannot figure out a way to keep the precision throughout the code


Any ideas would be very helpful

Quick look you have [price intValue] which will round to dollars.

what should I have instead?

I am not familiar with your notiation or SQLite but I think this:

....values('%@', %d, %d, '%@', '%@', '%@')", currencyName, [price intValue], [priceIncTAX intValue], validFrom, validTo, itemId];

should be replaced with this:

.....values('%@', %f, %f, '%@', '%@', '%@')", currencyName, [price floatValue], [priceIncTAX floatValue], validFrom, validTo, itemId];


where the %d and %d and intValue and intValue were changed to %f and %f and floatValue and floatValue

Accepted Answer

I then use this value and insert it in a Local SQLite DB using the following command

What does your database schema look like? Specifically, what is the type of the

price
column? In general you don’t want to use
REAL
for currency values because the rules of floating point arithmetic can result in significant rounding errors. In situations like this it is better to store the number of cents in an
INTEGER
column (that is, fixed point arithmetic).

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
Loosing precision of a number somewhere
 
 
Q