# Rounding a decimal number

Page 1 of 1

## 5 Replies - 1671 Views - Last Post: 01 February 2010 - 07:42 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=153383&amp;s=d714decb3ee43d87474f105212a1781f&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 dhuzie3

Reputation: 0
• Posts: 7
• Joined: 31-January 10

# Rounding a decimal number

Posted 01 February 2010 - 07:03 PM

I have been asked to write the following program....
"Write a program that prompts the user to enter a decimal number then outputs that number rounded to two decimal places""

I enter 50.926 and it outputs 50.98...Shouldn't it output as 50.93....What Am I doing wrong?

My compiler is bloodshed C++ Version 4.9.9.2

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
double x, num;

cout << fixed << showpoint << setprecision(2);

cout << "Enter a deicmal number: "; cin >> x;
num = (x + 0.05);

cout << "Rounded to two decimal places = " << num << endl;

system("pause");

return 0;
}

Is This A Good Question/Topic? 0

## Replies To: Rounding a decimal number

### #2 erik.price

• D.I.C Lover

Reputation: 486
• Posts: 2,690
• Joined: 18-December 08

## Re: Rounding a decimal number

Posted 01 February 2010 - 07:08 PM

Well, since you do this:
num = (x + 0.05);

The value of num will be 50.976 (50.926+0.05) before it is rounded with cout.

So yes, it is correctly rounded to 50.98.

Also, you may want to use the code tags in future posts. Thanks!

### #3 Astraeus

Reputation: 23
• Posts: 205
• Joined: 17-November 09

## Re: Rounding a decimal number

Posted 01 February 2010 - 07:08 PM

edit: Nevermind :x Erik got there first.

This post has been edited by Astraeus: 01 February 2010 - 07:09 PM

### #4 dhuzie3

Reputation: 0
• Posts: 7
• Joined: 31-January 10

## Re: Rounding a decimal number

Posted 01 February 2010 - 07:18 PM

Thanks....I'm very new to C++ so I appreciate your help...Please excuse my ignorance but code tags are what is in your brackets
...correct?

[quote name='erik.price' date='1 Feb, 2010 - 06:08 PM' post='912479']
Well, since you do this:
[code]num = (x + 0.05);

The value of num will be 50.976 (50.926+0.05) before it is rounded with cout.

So yes, it is correctly rounded to 50.98.

Also, you may want to use the code tags in future posts. Thanks!
[/quote]

Thanks anyway....

Astraeus, on 1 Feb, 2010 - 06:08 PM, said:

edit: Nevermind :x Erik got there first.

### #5 n8wxs

• --... ...-- -.. . -. ---.. .-- -..- ...

Reputation: 972
• Posts: 3,878
• Joined: 07-January 08

## Re: Rounding a decimal number

Posted 01 February 2010 - 07:24 PM

Quote

Thanks....I'm very new to C++ so I appreciate your help...Please excuse my ignorance but code tags are what is in your brackets

Code tags are these:
VVVVV_______________________________VVVVVV

### #6 dhuzie3

Reputation: 0
• Posts: 7
• Joined: 31-January 10

## Re: Rounding a decimal number

Posted 01 February 2010 - 07:42 PM

Got it.... Thanks.

n8wxs, on 1 Feb, 2010 - 06:24 PM, said:

Quote

Thanks....I'm very new to C++ so I appreciate your help...Please excuse my ignorance but code tags are what is in your brackets

Code tags are these:
VVVVV_______________________________VVVVVV