CRover CRover - 19 days ago 6
C++ Question

C++ - I want to select the first two digits after a decimal point and check to see if they are the same and not 0

I'm trying to get the first two digits after the decimal point of a number and the check to see if they are equal to each other and, at the same time, not equal to 0.

I know how to do the checking stuff but I have no idea how to select the first two digits after the decimal point.

Using setprecision would give me the number entirely, not just the two digits after the decimal point.

For example:

i = 3.141592
cout << setprecision(3) << i


would output 3.14, but I only want 14.

Answer

You could use the floor function from the std library

int(i*100 - (floor(i))*100)

Here is a good example webpage: http://en.cppreference.com/w/cpp/numeric/math/floor