S
S
Slavon72020-11-11 13:45:22
C++ / C#
Slavon7, 2020-11-11 13:45:22

My calculator displays an integer why?

My calculator displays an integer. And as planned, it should display a number with a comma and round up to 3 digits after the decimal point. I already changed to double and float, but I can’t % with them. And I need 123.656 -> 123/656 = 5.333

#include

using namespace std;

int main()
{
float a = 123.656f; // Change
int b, c, d;
float y; // Change
b = a * 1000;
c = b % 1000; // After the comma
d = b / 1000; // Before the comma

y = c / d;

cout << y ;
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
Wataru, 2020-11-11
@Slavon7

y = c / d;
You are dividing an integer by an integer. In the C language, in this case, there is an integer division. In order for the result to be a float, you need to convert one of the operands to float / double. You can either write it explicitly, or just add 0.0:
y = (c + 0.0) / d;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question