Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7030607
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:41:36+00:00 2026-05-28T00:41:36+00:00

For some reason, my following line of code is returning a 0. int bac

  • 0

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];

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T00:41:37+00:00Added an answer on May 28, 2026 at 12:41 am

    A couple things:

    • Make sure your data types are consistent. It is best to use 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:

    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 %.2f", bac];
    

    The formatter of %.2f says to display the float with 2 decimal places.

    • Order of operations. If the value is still incorrect, realize that (as written) each operation will be executed in order. If you need to group operations (like you really wanted to divide the entire first part by the entire second part), then add parentheses for grouping.

    In other words, possibly what you really meant was (although I’m not sure):

    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 %.2f", bac];
    

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason the following code doesn't work on Windows XP. new URL(file:// +
For some reason, the following code never calls Event::Event(Event&& e) Event a; Event b;
I have the following code: visitSite.hidden = YES; For some reason, when I click
For some reason the following C++ code results in a segmentation fault: #include <sstream>
For some reason, the following bit of code isn't behaving as I would anticipate
In my jquery i have the following code, which for some reason is not
For some reason the following code is giving me an exception. <Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries>
The following code for some reason poradically works. I have checked the URL so
I have the following method in some nemerle code: private static getLinks(text : string)
I'm following Michael Hartl's Rails Tutorial, and for some reason the following code: <%=

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.