Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Feb 2005
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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.

    Please help

    Thanks in advance
    west_dweller
    Attached Files Attached Files

  • #2
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    This almost looks like some sort of homework assignment? Is it?

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

  • #3
    Super Moderator sage45's Avatar
    Join Date
    May 2002
    Posts
    1,064
    Thanks
    0
    Thanked 15 Times in 15 Posts
    Well even still... It is simple enough...

    Code:
    cout << fixed << showpoint << setprecision(2);
    HTML & CSS Forum Moderator

    "If you don't know what you think you know, then what do you know."
    R.I.P. Derrick Thomas #58
    1/1/1967 - 2/8/2000

  • #4
    New Coder
    Join Date
    Feb 2005
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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??

  • #5
    Super Moderator sage45's Avatar
    Join Date
    May 2002
    Posts
    1,064
    Thanks
    0
    Thanked 15 Times in 15 Posts
    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-
    Last edited by sage45; 02-22-2005 at 01:52 AM.
    HTML & CSS Forum Moderator

    "If you don't know what you think you know, then what do you know."
    R.I.P. Derrick Thomas #58
    1/1/1967 - 2/8/2000

  • #6
    New Coder
    Join Date
    Feb 2005
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #7
    Super Moderator sage45's Avatar
    Join Date
    May 2002
    Posts
    1,064
    Thanks
    0
    Thanked 15 Times in 15 Posts
    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-
    HTML & CSS Forum Moderator

    "If you don't know what you think you know, then what do you know."
    R.I.P. Derrick Thomas #58
    1/1/1967 - 2/8/2000


  •  

    Posting Permissions

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