tags:

views:

36

answers:

3

I need to change the code below to make "intAmount" a decimal or an integer (i.e. a person can enter .10 or 1) in my uitextfield. The last line "myProduct" has to be a decimal not an integer and return the product in the format "18.00" for example. Can someone help someone help me alter my code snippit for this?

//amt has to be converted into a decimal value its a NSString now
NSInteger intAmount = [amt intValue];
//where total gets updated in the code with some whole (integer) value
NSInteger total=0;
//Change myProduct to a decimal with presicion of 2 (i.e. 12.65)
NSInteger myProduct=total*intAmount;

THIS DOESN'T WORK

NSDecimalNumber intAmount = [amt doubleValue];
//Keep in mind totalCost is an NSInteger
NSDecimalNumber total=totalCost*intAmount;
+3  A: 

Use doubleValue instead of intValue to get the correct fractional number out of your text field. Put it in a variable of type double rather than NSInteger. Then use the format %.2g when you print it out and it will look like you want it to.

Carl Norum
Carl can you write the code to print the format out
Nick LaMarca
Please look at my modified code above it doesn't work.
Nick LaMarca
you must use double not NSDecimalNumber. Still though %.2g wont put zeros in (i.e. if the total is 3.60 it reads 3.6 and if the total is 36 it reads 36, it should read 36.00)
Nick LaMarca
+2  A: 

If you need to track decimal values explicitly, you can use NSDecimalNumber. However, if all you're doing is this one operation, Carl's solution is most likely adequate.

Kevin Ballard
A: 
dreamlax