5 Replies - 928 Views - Last Post: 01 February 2010 - 07:42 PM Rate Topic: -----

#1 dhuzie3  Icon User is offline

  • New D.I.C Head

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

Rounding a decimal number

Post icon  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  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • 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! :code:
Was This Post Helpful? 0
  • +
  • -

#3 Astraeus  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 23
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#4 dhuzie3  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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! :code:
[/quote]


Thanks anyway....


View PostAstraeus, on 1 Feb, 2010 - 06:08 PM, said:

edit: Nevermind :x Erik got there first.

Was This Post Helpful? 0
  • +
  • -

#5 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • 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
:code:
Was This Post Helpful? 0
  • +
  • -

#6 dhuzie3  Icon User is offline

  • New D.I.C Head

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

Re: Rounding a decimal number

Posted 01 February 2010 - 07:42 PM

Got it.... Thanks.


View Postn8wxs, 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
:code:

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1