how do i get the decimal value to move postions?

not sure what data type to use to get proper values

Page 1 of 1

5 Replies - 2233 Views - Last Post: 02 August 2007 - 08:56 AM Rate Topic: -----

#1 wartech  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 203
  • Joined: 16-October 06

how do i get the decimal value to move postions?

Posted 01 August 2007 - 07:01 PM

Hi Everyone!
I have been struggling with a portion of my code. I need help understanding which data type to use to get the correct number output. Your help is appreciated.

ThankS!
#include <iostream>

using std::cin;
using std::cout;
using std::endl;

int main()
{

int num;

 cout << "F) Enter the value 612.14 " << endl;
  cin >> num;
  cout << "" << endl;
  cout << float(num) << endl; // I would like the  value to show as 612.1400
  cout << "" << endl;
  cout << double(num) << endl; // I would like the value to show as 6.1214E+02
  cout << endl;

   system ("Pause");
  return 0;

}



Is This A Good Question/Topic? 0
  • +

Replies To: how do i get the decimal value to move postions?

#2 Xing  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 19
  • View blog
  • Posts: 725
  • Joined: 22-July 06

Re: how do i get the decimal value to move postions?

Posted 01 August 2007 - 07:26 PM

Use double or float.
Was This Post Helpful? 0
  • +
  • -

#3 wartech  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 203
  • Joined: 16-October 06

Re: how do i get the decimal value to move postions?

Posted 01 August 2007 - 07:52 PM

Xing,
I have tried that but cannot get it to work.
I have also tried different datatypes for the variable "num"

double num;
long num;
float num;

I think that it is a combination of issues such as wrong datatype for the variable "num" and the wrong datatype for the output of "num".
Was This Post Helpful? 0
  • +
  • -

#4 barnwillyb  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 108
  • Joined: 22-May 07

Re: how do i get the decimal value to move postions?

Posted 01 August 2007 - 08:22 PM

View Postwartech, on 1 Aug, 2007 - 07:52 PM, said:

Xing,
I have tried that but cannot get it to work.
I have also tried different datatypes for the variable "num"

double num;
long num;
float num;

I think that it is a combination of issues such as wrong datatype for the variable "num" and the wrong datatype for the output of "num".


Try reading up on setiosflags.
Was This Post Helpful? 0
  • +
  • -

#5 BrainStew  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 154
  • Joined: 02-August 07

Re: how do i get the decimal value to move postions?

Posted 02 August 2007 - 05:10 AM

Try something like:

#include <iostream>
#include <iomanip>

using std::cin;
using std::cout;
using std::endl;

int main()
{

int num;

 cout << "F) Enter the value 612.14 " << endl;
  cin >> num;
  cout << "" << endl;
  cout << setprecision(4) << static_cast<float>(num) << endl; // I would like the  value to show as 612.1400
  cout << "" << endl;
  cout << setprecision(4) << static_cast<double>(num) << endl; // I would like the value to show as 6.1214E+02
  cout << endl;

   system ("Pause");
  return 0;

}



you don't need to use the c++ style static_cast, the old c style works fine, but I like it better.

This post has been edited by BrainStew: 02 August 2007 - 07:19 AM

Was This Post Helpful? 0
  • +
  • -

#6 wartech  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 203
  • Joined: 16-October 06

Re: how do i get the decimal value to move postions?

Posted 02 August 2007 - 08:56 AM

Thanks for the replies.

I stumbled across the answers I was looking for.
cout << "F) Enter the value 612.14 " << endl;
  cin >> num;
  cout << "" << endl;
  cout << fixed << endl; 
  cout << setprecision(5)<< num<< endl;
  cout << "" << endl;
  cout << scientific << endl;
  cout << setprecision(4) << num << endl;
  cout << endl;



This post has been edited by wartech: 02 August 2007 - 08:57 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1