...

Displaying value in 2 decimal form using C++

west_dweller79
02-21-2005, 06:06 PM
Dear all,

May i know how to display value in 2 decimal form using C++? Attach is my program which just display the integer value instead of 2 decimal.

west_dweller

oracleguy
02-21-2005, 09:50 PM
This almost looks like some sort of homework assignment? Is it?

And to clarify, you want it to display like 3 as 3.00?

sage45
02-22-2005, 01:33 AM
Well even still... It is simple enough...

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

west_dweller79
02-22-2005, 01:57 AM
Yes, i'll like to show it as 0.00 (for 0) and 5.00 (for 5). By the way,

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

So wat does fixed, showpoint mean?? And for setprecision(2), do i need to include and header file or library??

sage45
02-22-2005, 02:50 AM
You've already included the iomanip file right?

If so then you've done everything you need for that line of code right there...

cout << fixed - sets the output to a fixed display:

Output using default settings
0 0
1 3.14159
2 50.2655
3 254.469
4 804.248
5 1963.5
6 4071.50
7 7542.96
8 12868
9 20612
10 31415.9
11 45996.1
12 65144.1
13 89727.0
14 120687

Output using fixed format, setprecision 2
0 0.00
1 3.14
2 50.27
3 254.47
4 804.25
5 1963.50
6 4071.50
7 7542.96
8 12867.96
9 20611.99
10 31415.93
11 45996.06
12 65144.07
13 89727.03
14 120687.42

cout << showpoint, tells the output stream to display the decimal point for whole numbers and some trailing zeros... setprecision then limits the trailing zeros to the amount specified...

edit: Lost the formatting within the forums formatting... Just google it -
http://www.eecs.umich.edu/~kieras/eecs280w00website/lecture/formatting.pdf
http://www.informit.com/articles/article.asp?p=170770&seqNum=3

-sage-

west_dweller79
02-23-2005, 07:11 PM
Hi Sage45. i wrote the code in the following way, but still couldn't get any decimal point.

//y is between 0 - 255

cout << y/51 << showpoint <<setprecision(3);

y is the variable that use to get the value from other function and it's declare as char. Is it possible to get the value in decimal form with variable declare as char? Cause my assignment need me to set it as char.

west_dweller

sage45
02-24-2005, 02:01 AM
Implemented wrong... Try this instead...

//y is between 0 - 255

cout << showpoint <<setprecision(3);
cout << y/51;

Note: showpoint and setprecision will apply to all formatted output after they are entered... In order to go back to normal formatting you have to reset them when finished...

-sage-

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum