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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T02:49:33+00:00 2026-06-03T02:49:33+00:00

I have a problem. I need to get two numbers and add them together

  • 0

I have a problem. I need to get two numbers and add them together in a way that one is the decimal point of the other. For example:

double number1 = 20;
double number2 = 142;

I need to add these numbers together to get 20.142. Is there a way to do this?

Thanks

  • 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-03T02:49:35+00:00Added an answer on June 3, 2026 at 2:49 am
    bool negative = (number1 < 0.0);
    
    if (negative)
        number1 = 0 - number1;
    while (number2 >= 1.0)
        number2 = number2 / 10.0;
    
    double finalnum = number1 + number2;
    if (negative)
        finalnum = 0 - finalnum;
    

    It’s quite simple. We first determine if number1 is negative, and if so, make it positive and set a boolean flag. Then we just keep dividing number2 by 10.0 until it is less than 1.0 (which means it’s now on the range [0, 1) assuming it’s positive). Then we just add the two numbers together and store it in finalnum, and if negative is true (YES), negate finalnum.

    This solution could have also been solved using base-10 logarithms to determine the amount to divide number2 by, but that most likely will take longer.

    You could also have solved this by turning those numbers into const char* or CFString (Objective-C) strings and then concatenate them followed by an interpretation of the resulting string as a double, but again, this will take longer.

    There are two caveats to this code:

    1. number1 and number2 must be integers (if number1 was, say, 1.5 and number2 was 25.7, finalnum would be 1.757)
    2. number2 must be positive
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a problem with sql query. I need to get list of users.
I have a big problem with very simple code. I need to get a
I need to use something like get_or_create() but the problem is that I have
I need the most efficient way (in cpu cycles) to determine if two numbers
I have two text fields in which numbers are inputed as strings. I need
I have problem i need to convert from my Array structure to std::vector<int> ...
I have a problem i need to reference a single foreign key to a
I have a problem where I need to remove all code and triggers from
I have the following problem: I need to use XSLFO to generate a 2-column
I have the next problem: I need to process only 1 request at a

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.