For some reason, my following line of code is returning a 0.
int bac = [food.text intValue] * 12 * 0.042 * 5.14 / [weight.text intValue] * 0.73;
I can’t seem to figure out why. food and weight are both UITextFields. Any help would be very much appreciated.
Does it have anything to do with the int needing to be a float? or a double?
Here is my entire method:
-(IBAction) calcBac {
float bac = [food.text floatValue] * 12 * 0.042 * 5.14 / [weight.text floatValue] * 0.73;
NSString *msg = [[NSString alloc] initWithFormat: @"Results: You're BAC is %d", bac];
result.text = msg;
[msg release];
}
A couple things:
float, so that you don’t lose fractional parts in the computation, but you can’t turn around and try to display it with an int formatter (%d).So, to clean up your example:
The formatter of
%.2fsays to display the float with 2 decimal places.In other words, possibly what you really meant was (although I’m not sure):
Without any grouping, running the numbers on a calculator for the 5/160 example you gave, I get a result of ~0.059. If that’s not the output you are looking for, you may be missing some grouping symbols.
HTH