# Round number to n decimal places

Page 1 of 1

## 0 Replies - 2238 Views - Last Post: 09 July 2012 - 05:24 PMRate Topic:     //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=364366&amp;s=cd1866d0781ffde3fd5e510ac24cde3b&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 atraub Reputation: 833
• Posts: 2,257
• Joined: 23-December 08

# Round number to n decimal places

Posted 09 July 2012 - 05:24 PM

Description: Given a double "a", and an integer "n", this function will round the double to n decimal places. This solution is not optimal, but it does what it needs to do.
```#include <math.h>

//double a - number to round
//int n - number of decimal places to round to
double nDecimalPlaces(double a, int n)
{
return round(a*pow(10,n))/pow(10,n);
}
```

Is This A Good Question/Topic? 0

## Replies To: Round number to n decimal places

### #2 atraub Reputation: 833
• Posts: 2,257
• Joined: 23-December 08

## Re: Round number to n decimal places

Posted 09 July 2012 - 05:24 PM

Description: Given a double "a", and an integer "n", this function will round the double to n decimal places. This solution is not optimal, but it does what it needs to do.
```#include <math.h>

//double a - number to round
//int n - number of decimal places to round to
double nDecimalPlaces(double a, int n)
{
double pow10 = pow(10,n);
return round(a*pow10)/pow10;
}

```

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }