C ++ program defaults to rounding

I am currently working on a C ++ program where I have a bankAccount class and I need to calculate the percentage. The problem is that my function rounds my numbers to an integer, although I use float as my variable type. Therefore, if I had code like this:

float BankAccount::CalculateInterest(int Time)
{
    float thing;
    thing = (967 / 365);
    return thing;
}

a thing ends with 2.00000000, when it should be 2.649315068.

Any ideas? I used to have my equation of interest, and the result was always 1.00000000, no matter what, so I put it as a test, and I saw that it was rounded to the floor.

Thank you for your help!

+4
source share
2 answers

This is not rounding.

, .

, ".0" float.

+4

967 365 , . : 967 / 365.0.

:

float thing = 967;  // integer converted to float during assignment
thing /= 365;  // division of float and converted integer
+3

Source: https://habr.com/ru/post/1616296/


All Articles