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

The Archive Base Latest Questions

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

Let’s say I want to write a function that does the following: Given a

  • 0

Let’s say I want to write a function that does the following:

Given a number N,
when N is rounded to D1 digits
does the result include more than D2 decimal places, not counting trailing zeroes?

For example, say N is .01001, D1 is 4, and D2 is 2. The question becomes, does .0100 include more than 2 decimal places, not counting trailing zeroes? And the answer is “no”. But if N was .00101, the answer would be “yes”.

I’m looking for an efficient way to do this using standard C library functions, taking into account the limitations of floating-point numbers.

An example of my intended usage: show a number using four digits if necessary, but otherwise show it using two digits.

(This is not a homework question — this is a result of being a professional programmer who didn’t do these kinds of homework questions when he was a student.)

  • 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-17T00:28:45+00:00Added an answer on May 17, 2026 at 12:28 am

    The only easy way to do this with the standard library is to use snprintf (or just sprintf) with the right format, and then count the zeros yourself. Correctly computing the decimal representation of a (binary) floating point number is a very very difficult task that you don’t want to try to do yourself, and you have near-zero chance of writing a correct version that’s faster than your standard library one.

    I hope I got this right; it’s untested:

    double n; /* the number N */
    int d1, d2; /* the parameters d1 and d2 */
    char s[MAXLEN], *z;
    snprintf(s, sizeof s, "%.*f", d1, n);
    for (z=s+strlen(s)-1; *z==0; z--);
    if (strlen(++z)<d1-d2) puts("yes");
    else puts("no");
    

    Edit: As noted by ssianky, snprintf may have limitations on the precision it prints. Actually the C standard allows pretty much any floating point operation to give the wrong result for no reason whatsoever as long as the implementation documents as such, but IEEE behavior is encouraged, and POSIX additionally requires correctly rounded results up to DECIMAL_DIG places, but allows implementations to print nonsense (for example all zeros) after printing sufficiently many places to uniquely determine the actual floating point value. So to make a long story short, if d1 is reasonably large, or if your platform is pathological, snprintf-based approaches might not give the right answer. (In practice they’ll give the right answer on GNU systems and the wrong answer on Microsoft systems.)

    If you care about this shortcoming and want correct results for any value of d1, you’ll have to implement exact float-to-decimal code yourself. Loops that involve multiplying a floating point value repeatedly by 10 will not suffice for this.

    Edit 2: Looking at the OP’s “intended usage”, using snprintf seems like a no-brainer. If you want to print the value to begin with and are just trying to decide how many decimal places to use, just print it to a string and then chop off the trailing zeros before displaying it. In fact, the %g printf format specifier might even do what the OP wants already…

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

Sidebar

Related Questions

Let's say I have the following function in C#: void ProcessResults() { using (FormProgress
Let's say I want to have some kind of a cache that did something
Let's say I don't have photoshop, but I want to make pattern files (.pat)
Let me explain best with an example. Say you have node class that can
Let's say that I have a SQLite database that I create in a separate
Let's say I have thousands of users and I want to make the passwords
Let's say I have the following text: (example) <table> <tr> <td> <span>col1</span> </td> <td>col2</td>
Let's say I have the following object: var VariableName = { firstProperty: 1, secondProperty:
Let's say I have multiple requirements for a password. The first is that the
Let's say that I have a date in R and it's formatted as follows.

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.