Thread: Formatting number output

  1. #1
    Registered User
    Join Date
    Jan 2019
    Posts
    3

    Formatting number output

    Hi everyone,

    If I have this code

    Code:
    int millisec = 4562
    int seconds = (millisec / 1000);
    The output will be 4.562. How can I get the output to be 4.5


    Thanks

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,337
    The output will be 4.562. How can I get the output to be 4.5
    1. No, there is no output in the snippet posted.

    2. No, since both of your variables are integers the answer should be 4. Remember that there are no fractions when doing integer math.

  3. #3
    Registered User
    Join Date
    Jan 2019
    Posts
    3
    Quote Originally Posted by jimblumberg View Post
    1. No, there is no output in the snippet posted.

    2. No, since both of your variables are integers the answer should be 4. Remember that there are no fractions when doing integer math.
    I was using a calculator to get my output as I am trying to make this as simple as possible.

    I guess my question should have been how can I take those two numbers and get that specific output.

  4. #4
    Registered User john.c's Avatar
    Join Date
    Dec 2017
    Posts
    588
    One way is to use a floating point variable (double is the common choice) and set cout to print a fixed number of fractional digits with that number (precision) being 1:
    Code:
    #include <iostream>
    #include <iomanip>
     
    int main() 
    {
        int millisec = 4562;
     
        // Must be floating point to have a fractional part.
        // Note that the calculation must have at least one floating point value
        // (1000.0 in this case) for the calculation to be floating point.
        double seconds = (millisec / 1000.0);
     
        // Set cout to a fixed number of fractional digits
        // In fixed mode, the precision indicates the number of fractional digits.
        std::cout << std::fixed << std::setprecision(1);
     
        std::cout << seconds << '\n';  // prints 4.6
    }
    Note that this rounds up.

    Alternatively you can do it with just integer math:
    Code:
    #include <iostream>
     
    int main() 
    {
        int millisec = 4562;
     
        // Without rounding (prints 4.5)
        std::cout << millisec / 1000 << '.' << millisec % 1000 / 100 << '\n';
     
        // With rounding (prints 4.6)
        std::cout << millisec / 1000 << '.' << (millisec + 50) % 1000 / 100 << '\n';
    }
    Let him who is not come to logic be plagued with continuous and everlasting filth.
    - John of Salisbury, 1160

  5. #5
    Registered User
    Join Date
    Jan 2019
    Posts
    3
    Thanks John, this definitely helps me out.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. formatting output
    By kisiellll in forum C Programming
    Replies: 2
    Last Post: 04-04-2009, 03:53 PM
  2. Formatting Output
    By Aakash Datt in forum C++ Programming
    Replies: 2
    Last Post: 05-16-2003, 08:20 PM
  3. Help with output formatting
    By webvigator2k in forum C++ Programming
    Replies: 1
    Last Post: 04-14-2003, 09:16 PM
  4. formatting output
    By z.tron in forum C++ Programming
    Replies: 5
    Last Post: 11-22-2002, 06:11 PM
  5. formatting output
    By spliff in forum C Programming
    Replies: 2
    Last Post: 08-14-2001, 06:50 PM

Tags for this Thread