1

Basically if I write 15.20 I want 15.20 to return and if I write without any extra zeros or any rounding so that the 0 is still printed out. I tried using setprecision, but it just adds a bunch of zeros.

my code:

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
  double flyttal; 
  cout << "Skriv in ett flyttal: ";
  cin >> flyttal;
  cout << "Du skrev in flyttalet: " << fixed << setprecision(2) << flyttal << endl;

  return 0; 
}

Now thats fine for the float value 15.20 but if I want to write 1.315456 I will only get 1.32. I want to be able to get the same amount of decimals as I put in.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
Zeptuz
  • 103
  • 7
  • c++ has no decimal type, and floating point values are represented by IEEE-754 standard (https://en.wikipedia.org/wiki/IEEE_754). What you think you "put it" (e.g. 1.315456) is not necessarity what is actually stored in the variable, since not all decimal fractions can be represented as IEEE-754 values. – wohlstad Nov 28 '22 at 18:01
  • 1
    The problem is that you can't really know how many decimals there are in a floating-point value. For example, some values can't be precisely represented in a floating-point type, so it will be rounded to the closest value possible, which might lead to many extra decimals that are unexpected. Would you still want to count them? – Some programmer dude Nov 28 '22 at 18:02
  • 4
    a `double` does not adjust its precision according to user input. A `std::string` can do what you want: read a string, parse it to get a `double`, but remember the precision in the user input, then print the `double` accordingly. Or simply read and print a `std::string` – 463035818_is_not_an_ai Nov 28 '22 at 18:03
  • 2
    If you for sure want hundredths, I'd suggest using an integer type internally and adding the two decimal points on display. (or a string like the previous comment, but integers are better for doing sums and stuff) We call it integer pennies when working with money, where you do not want rounding errors. – Kenny Ostrom Nov 28 '22 at 18:03
  • https://stackoverflow.com/questions/588004/is-floating-point-math-broken?r=Saves_AllUserSaves – πάντα ῥεῖ Nov 28 '22 at 18:07
  • Second the suggestion to just read in a string and print out a string. That will satisfy your _current_ use case. If you need to perform computations on decimal numbers, prefer using integers and do math directly on the smallest denomination (for example, store all money as øre instead of krone). Your use case will inform the best approach. – JohnFilleau Nov 28 '22 at 18:10
  • Floating point is usually only the correct answer when dealing with physics simulations or control systems. Use fixed point for all others (an integer is a fixed point). Using floating point for time or money can kill people or lead to the plot of Superman 2. – JohnFilleau Nov 28 '22 at 18:12
  • A floating point number has no way to store how many digits you entered after the decimal point. On top of this it does not even have the ability to represent exact floating point values like `0.3` If you need to store the precision in your code you may have to read the number as a string. This calculator will show you what is actually stored for a float: [https://www.h-schmidt.net/FloatConverter/IEEE754.html](https://www.h-schmidt.net/FloatConverter/IEEE754.html) – drescherjm Nov 28 '22 at 18:15
  • Thanks to everyone who replied. This is literally my first few lines of code in C++ so I pretty much dont know anything. But I will try and read the number as string and that should probably do it. – Zeptuz Nov 28 '22 at 18:29

0 Answers0