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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:55:53+00:00 2026-05-31T18:55:53+00:00

I need to calculate the number of decimal places for a float value, e.g.

  • 0

I need to calculate the number of decimal places for a float value, e.g.

1234.567 -> 3
2.1233 -> 4
4.2432 -> 4

My initial idea was:

number = 1234.567;  
...  
while (number - (int)number > 0.0)  
{  
  // Count decimal places  
  ...  
  number *= 10;  
}

However, this causes problems with float precision in the while-condition. The only safe workaround would be a conversion from float to string and then do string-based counting of decimal places.

The problem is: I must NOT use any libraries, neither third party nor C++ standard libraries (due to environmental restrictions). I know how to operate on a char* later on, but how can I convert my float value to a string (i.e. char*) without using C++ libraries?

Any help is greatly appreciated.


// Edit: This is my current approach, which still does not work (e.g. for 2.55555). How do I choose a proper threshold?

float abs(float number)  
{  
  return (number > 0.0 ? number : number * -1);  
}  

int round(float number)  
{  
  return (int)(number + 0.5);  
}  

void splitFloat(float* number, int* mantissa, int* exponent)  
{  
  while (abs(*number - round(*number)) > 0.00001)  
  {  
    // *number -= (int)*number; // ???  
    *number *= 10.0;  

    *mantissa = *number;  
    *exponent += 1;  

    cout << "Number: " << *number << ", Mantisse: " << *mantissa << ", Exponent: " << *exponent << endl;  
  }  
}
  • 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-31T18:55:54+00:00Added an answer on May 31, 2026 at 6:55 pm

    Your initial idea is pretty close, the problem is that floating point does rounding that keeps the result from being exact. You need to use a threshold instead of comparing to exactly 0.0, and you need to allow that the (int) operation might truncate incorrectly and you should round instead. You can round by adding 0.5 before truncating.

    You’ll also run into a problem when the number of digits doesn’t fit into an int anymore. You can help by subtracting off the integer part of the number at each step.

    Edit: To choose a proper threshold, pick the maximum number of decimals you want to process. If that’s 4, then the smallest number you want to output is 0.0001. Make your threshold half of that, or 0.00005. Now, every time you multiply your number by 10, multiply the threshold by 10 too!

    float threshold = 0.00005;
    while (abs(*number - round(*number)) > threshold)  
    {  
      *number *= 10.0;
      threshold *= 10.0;
      // ...
    }
    

    If your float and int are both 32 bits, you shouldn’t have to worry about subtracting out the int. It would make it harder to return the mantissa as you’re doing.

    Also, a warning I meant to give you before but forgot: this only works for positive numbers.

    One more warning, the range of values for a float is quite limited. You might not be able to exactly represent 1234.5670 for example, and you’ll get an extraneous digit on the end. Changing to double would fix that.

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

Sidebar

Related Questions

Seems, it's common task, but I haven't found solution. I need to calculate number
I need to calculate the number of FULL month in SQL, i.e. 2009-04-16 to
I need to calculate (in R) the number of positive and negative runs in
I need to calculate an end date based on todays date and a number
I need to calculate the number of images stored in SD card and so
I have time represented by a decimal number that I need to convert to
i need to calculate the number of workdays between two dates. a workday is
I need to calculate the number of seconds that have passed between two events
I need to calculate the number of days for a given month in python.
I have a 15 digit customer number that I need to calculate a check

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.