Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

[C++] My first IT course with simple output

  1. Jun 15, 2015 #1
    Hi, my C++ course has started since last week and I am learning basic input and output.
    In my simple project, I have a function that will return a double number like this
    Code (C):

    double getLong(long long ll)
        return ll/10.00;
    The input is 12345 and the return value after the code is executed is 1234.500000
    I find it strange as to why 0's are added unexpectedly.
    I create a console application and test again with main like this
    Code (C):

    int main()
    But the result becomes 1234.5 which is correct.

    Another case I run with my function is if I input e.g 1234567890 then I expect the result should be 123456789.0 (I don't want the compiler to automatically delete my 0 after the decimal point). How can I achieve this ? Thank you a lot.
  2. jcsd
  3. Jun 15, 2015 #2


    Staff: Mentor

    When you print your number cout uses a default which escapes me at the moment but which I suspect is a variable format which supresses trailing zeros.

    However, you can adjust how numbers are printed:

  4. Jun 15, 2015 #3


    User Avatar

    Staff: Mentor

    How do you know what the return value is?
  5. Jun 15, 2015 #4
    I see the values in the debugger.
    By the way I fixed it using the tutorial link in jedishrfu's post. I learned new thing in C++, setw and set precision. My teacher seemingly forgot them in his previous lecture.
  6. Jun 15, 2015 #5


    User Avatar

    Staff: Mentor

    The (default) number of decimal places to show is a decision by the authors of the debugger. There might be an option in the debugger's preferences section to change this.
  7. Jun 15, 2015 #6
    Really ? I don't know if there is.
  8. Jun 17, 2015 #7


    User Avatar
    Gold Member

    How exactly is the numerical value 1234.500000 any less correct than 1234.5? Or 1234.500 for that matter?

    The output of your program (the text string "1234.5") is what is important and that is correct.

  9. Jun 18, 2015 #8
    Thank you BoB for your insight. Yes, it should be that.
    I'm working on a financial application so I need to read from and write to the file the exact values.
  10. Jun 18, 2015 #9

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    You need to be very careful when using floating point to represent money.
    Code (C):

    #include <iostream>
    int main ()
       double x = 0.10;
       double y = 0.20;
       double z = 0.30;
       if ((x+y) != z)
          std::cout << "This doesn't seem right!\n"
                    << x << " + " << y << " - " << z << " = " << x+y-z << '\n';
    On most computers, this will print
    Code (Text):

    This doesn't seem right!
    0.1 + 0.2 - 0.3 = <some small but non-zero number>
    Last edited: Jun 18, 2015
  11. Jun 29, 2015 #10
    The best way to be careful with floating point and currency is not to use floating point for currency.

    You can use integers and count cents, or you can look at fixed precision or decimal libraries. If you can't find anything better, there's cpp_dec_float in boost. I believe that in commercial code some juristictions require the use of decimal libraries for currency.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook