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

  • Home
  • SEARCH
  • 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 9005645
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:12:02+00:00 2026-06-16T01:12:02+00:00

I am trying to divide two integer values and store as a float. void

  • 0

I am trying to divide two integer values and store as a float.

void setup()
{
    lcd.begin(16, 2);

    int l1 = 5;
    int l2 = 15;
    float test = 0;
    test = (float)l1 / (float)l2;

    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print(test);
}

For some reason that I expect is fairly obvious I can’t seem to store and display the correct value. ‘test’ variable is always set to 0.

How do I cast the integer values?

  • 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-06-16T01:12:03+00:00Added an answer on June 16, 2026 at 1:12 am

    It must be your LCD print routine, and thus the casts you have used are correct.

    I tried it out on an Arduino using serial printing instead of an LCD. The expected result appears in the serial monitor (started by menu Tools -> Serial Monitor) for the complete code example below:

    Start...
    
    5
    15
    0.33
    0.33333334922790
    

    The last result line confirms it is a 4 byte float with 7-8 significant digits.

    Complete code example

    /********************************************************************************
     * Test out for Stack Overflow question "Divide two integers in Arduino",       *
     * <http://stackoverflow.com/questions/13792302/divide-two-integers-in-arduino> *
     *                                                                              *
     ********************************************************************************/
    
    // The setup routine runs once when you press reset:
    void setup() {
        // Initialize serial communication at 9600 bits per second:
        Serial.begin(9600);
    
        //The question part, modified for serial print instead of LCD.
        {
            int l1 = 5;
            int l2 = 15;
            float test = 0;
            test = (float)l1 / (float)l2;
    
            Serial.println("Start...");
            Serial.println("");
            Serial.println(l1);
            Serial.println(l2);
            Serial.println(test);
            Serial.println(test, 14);
        }
    
    } //setup()
    
    void loop()
    {
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having an issue trying to divide two decimals and then display the result.
I am trying to divide two image widths in a Bash script, but bash
I'm trying to divide a string into two columns with 6 lines in each.
I am trying to divide two variables in Jquery as follows: var image_width =
I'm trying to multiply (add/divide/etc.) two dataframes that have different column labels. I'm sure
I'm trying divide two numbers in assembly. I'm working out of the Irvine assembly
I'm trying to divide two fields (where sometimes the divisor might be zero) with
I'm trying to divide two integers and multiply by 100 but it keeps giving
I am trying to use php's explode to divide a string into two. However
I am trying to divide two numbers, a numerator N by a divisor D.

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.