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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T07:51:17+00:00 2026-06-16T07:51:17+00:00

Possible Duplicate: Rounding Number to 2 Decimal Places in C I have not found

  • 0

Possible Duplicate:
Rounding Number to 2 Decimal Places in C

I have not found a function with a signature double round(double d, int digits) like here in c.
When i try to build i get a error:

error: too many arguments to function ’round’

How can I round in C with N digits after the decimal point?

  • 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-16T07:51:19+00:00Added an answer on June 16, 2026 at 7:51 am

    Using recursion (which is going to be slow for some values of digits)

    #include <math.h>
    double my_round(double x, unsigned int digits) {
      if (digits > 0) {
        return my_round(x*10.0, digits-1)/10.0;
      }
      else {
        return round(x);
      }
    }
    

    A method likely to be somewhat faster, but which relies on a single call to the slow pow function:

    #include <math.h>
    
    double my_round(double x, unsigned int digits) {
        double fac = pow(10, digits);
        return round(x*fac)/fac;
    }
    

    An even faster method is to precompute a lookup table with the likely powers and use that instead of pow.

    #include <math.h>
    
    double fac[];  // population of this is left as an exercise for the reader
    
    double my_round(double x, unsigned int digits) {
        return round(x*fac[digits])/fac[digits];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: C# Double - ToString() formatting with two decimal places but no rounding
Possible Duplicate: Truncate (not round) decimal places in SQL Server Can't figure this out.
Possible Duplicate: Moving decimal places over in a double I am creating a basic
Possible Duplicate: Display two decimal places, no rounding I need to format the total
Possible Duplicate: how to make “pretty rounding” in R? I have a number, say
Possible Duplicate: C++ double precision and rounding off Code: int main(void) { double a
Possible Duplicate: Can main function call itself in C++? I found this problem very
Possible Duplicate: Why does my cout output not appear immediately? I have a very
Possible Duplicate: Is JavaScript’s Math broken? I'm using Math.round to round the number and
Possible Duplicate: Create Excel (.XLS and .XLSX) file from C# Rounding double values in

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.