Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: Displaying value in 2 decimal form using C++

1. ## Displaying value in 2 decimal form using C++

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

• This almost looks like some sort of homework assignment? Is it?

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

• Well even still... It is simple enough...

Code:
`cout << fixed << showpoint << setprecision(2);`

• 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??

• 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:
Code:
```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/ee...formatting.pdf
http://www.informit.com/articles/art...70770&seqNum=3

-sage-

• 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

• 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-

•

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•