Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,190 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,127 people online right now. Registration is fast and FREE... Join Now!




how do i get the decimal value to move postions?

 
Reply to this topicStart new topic

how do i get the decimal value to move postions?, not sure what data type to use to get proper values

wartech
1 Aug, 2007 - 06:01 PM
Post #1

D.I.C Head
Group Icon

Joined: 16 Oct, 2006
Posts: 106



Thanked: 3 times
My Contributions
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!
CODE

#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;

}

User is offlineProfile CardPM
+Quote Post

Xing
RE: How Do I Get The Decimal Value To Move Postions?
1 Aug, 2007 - 06:26 PM
Post #2

D.I.C Addict
Group Icon

Joined: 22 Jul, 2006
Posts: 723



Thanked: 2 times
Dream Kudos: 1575
My Contributions
Use double or float.
User is offlineProfile CardPM
+Quote Post

wartech
RE: How Do I Get The Decimal Value To Move Postions?
1 Aug, 2007 - 06:52 PM
Post #3

D.I.C Head
Group Icon

Joined: 16 Oct, 2006
Posts: 106



Thanked: 3 times
My Contributions
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".


User is offlineProfile CardPM
+Quote Post

barnwillyb
RE: How Do I Get The Decimal Value To Move Postions?
1 Aug, 2007 - 07:22 PM
Post #4

D.I.C Head
**

Joined: 22 May, 2007
Posts: 55


My Contributions
QUOTE(wartech @ 1 Aug, 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".


Try reading up on setiosflags.
User is online!Profile CardPM
+Quote Post

BrainStew
RE: How Do I Get The Decimal Value To Move Postions?
2 Aug, 2007 - 04:10 AM
Post #5

D.I.C Head
**

Joined: 2 Aug, 2007
Posts: 149



Thanked: 1 times
My Contributions
Try something like:

CODE

#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: 2 Aug, 2007 - 06:19 AM
User is offlineProfile CardPM
+Quote Post

wartech
RE: How Do I Get The Decimal Value To Move Postions?
2 Aug, 2007 - 07:56 AM
Post #6

D.I.C Head
Group Icon

Joined: 16 Oct, 2006
Posts: 106



Thanked: 3 times
My Contributions
Thanks for the replies.

I stumbled across the answers I was looking for.
CODE

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: 2 Aug, 2007 - 07:57 AM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/2/08 01:23AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month