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 8358289
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T10:44:54+00:00 2026-06-09T10:44:54+00:00

How can I convert two unsigned integers that represent the digit and decimal part

  • 0

How can I convert two unsigned integers that represent the digit and decimal part of a float, into one float.

I know there are a few ways todo this, like converting the decimal component into a float and multiplying it to get a decimal and added it to the digit, but that does not seem optimal.

I’m looking for the optimal way todo this.

/*
 * Get Current Temp in Celecius.
 */
void  GetTemp(){


    int8_t digit = 0;      // Digit Part of Temp
    uint16_t decimal = 0;  // Decimal Part of Temp

    // define variable that will hold temperature digit and decimal part
    therm_read_temperature(&temperature, &decimal); //Gets the current temp and sets the variables to the value

}

I want to take the Digit and Decimal parts and convert them to a float type, such that it looks like digit.decimal .

It might look like this in end, but I want to find the MOST optimal solution.

/*
 * Get Current Temp in Celecius.
 */
float GetTemp(){


    int8_t digit = 0;      // Digit Part of Temp
    uint16_t decimal = 0;  // Decimal Part of Temp

    // define variable that will hold temperature digit and decimal part
    therm_read_temperature(&temperature, &decimal); //Gets the current temp and sets the variables to the value

    float temp = SomeFunction(digit, decimal);  //This could be a expression also. 

    return temp;

}

////UPDATE/// – July 5th

I was able to get the source code instead of leveraging just the library. I posted it in this GIST DS12B20.c.

    temperature[0]=therm_read_byte();
    temperature[1]=therm_read_byte();
    therm_reset();

    //Store temperature integer digits and decimal digits
    digit=temperature[0]>>4;
    digit|=(temperature[1]&0x7)<<4;
    //Store decimal digits
    decimal=temperature[0]&0xf;
    decimal*=THERM_DECIMAL_STEPS_12BIT;

    *digit_part = digit;
    *decimal_part = decimal;

Although the function will not force us to return separate parts as digit and decimal, reading from the temperature sensor seems to require this (unless i’m missing something and it can be retrieved as a float).

I think the original question still stands as what is the optimal way to make this into a float in C (this is for use with AVR and an 8bit microprocessor, making optimization key) using the two parts or to be able to retrieve it directly as a float.

  • 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-09T10:44:57+00:00Added an answer on June 9, 2026 at 10:44 am

    What you are really running into is using fixed-point numbers. These can be represented in two ways: either as a single integer with a known magnitude or multiplier (ie. “tenths”, “hundredths”, “thousandths”, and so on; example: value from a digital scale in ten-thousandths of a gram, held in a 32-bit integer — you divide by 10000 to get grams), or as two integers, with one holding the “accumulated” or “integer” value, and the other holding the “fractional” value.

    Take a look at the <stdfix.h> header. This declares types and functions to hold these fixed-point numbers, and perform math with them. When adding fractional parts, for example, you have to worry about rolling into the next whole value, for which you then want to increment the accumulator of the result. By using the standard functions you can take advantage of built-in processor capabilities for fixed-point math, such as those present in the AVR, PIC and MPS430 microcontrollers. Perfect for temperature sensors, GPS receivers, scales (balances), and other sensors that have rational numbers but only integer registers or arithmetic.

    Here is an article about it: “Fixed Point Extensions to the C Programming Language”, https://sestevenson.wordpress.com/2009/09/10/fixed-point-extensions-to-the-c-programming-language/

    To quote a portion of that article:

    I don’t think the extensions simplify the use of fixed types very
    much. The programmer still needs to know how many bits are allocated
    to integer and fractional parts, and how the number and positions of
    bits may change (during multiplication for example). What the
    extensions do provide is a way to access the saturation and rounding
    modes of the processor without writing assembly code. With this level
    of access, it is possible to write much more efficient C code to
    handle these operations.

    Scott G. Hall
    Raleigh, NC, USA

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

Sidebar

Related Questions

Are there any libraries (available in Maven Central) that can convert diff output to
Is there a way that I can convert the to_be_approved by method in the
I know the deal .The compiler tries to convert one object into the other
There's this program, pdftotext, that can convert a pdf file to a text file.
Motivation: I would like to convert hashes (MD5/SHA1 etc) into decimal integers for the
How can convert my time received from my $_GET variables as two digit values
Is there an existing class in C# that can convert Quoted-Printable encoding to String
is there a function in abap can convert digit to word? example: 100 will
So far I've discovered I can convert incoming BSTRs to ANSI in two (of
How can I convert this type of Arraylist to Two Dimensional Arraylist ? ArrayList<ArrayList<String>>

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.