[SOLVED] Problem with dividing two numbers

Hello. At the beginning sorry for my english, I hope that we will be able to understand each other :slight_smile:
I've programmed ages ago, but this problem has broken me. The idea of my project is reads some data from serial and then show it on oled screen. One of the datas begins with "SD print" and then goes two big numbers with slash inside them. I have got divide the string and extract the two numbers then I have converted it to int and finally i want to divide them to get percents at the end. And here is the problem i can't divide, always get "0", I can add or subtract. Any idea what I'm doing wrong?

String data = "",czasTeraz, czasCaly;;
unsigned long CYFczasTeraz, CYFczasCaly;
unsigned long procent;
int pomoc=0;

void setup()
{
Serial.begin(115200);
}

void loop() // SD print 60/100 <-example of data
{
if (Serial.available() > 0)
{

if(Serial.find ("SD print"))
{
data = (Serial.readStringUntil("/n"));
Serial.println(data);
Serial.flush();

data.trim();
Serial.println(data);

int poz = data.indexOf('/');
Serial.println(poz);

czasTeraz = data.substring(0,poz);
czasCaly = data.substring(poz+1);
Serial.println(czasTeraz);
Serial.println(czasCaly);
Serial.println(czasTeraz.length());
Serial.println(czasCaly.length());

CYFczasTeraz = czasTeraz.toInt();
CYFczasCaly = czasCaly.toInt();
Serial.println(CYFczasTeraz);
Serial.println(CYFczasCaly);
Serial.println(CYFczasTeraz+CYFczasCaly);
Serial.println(CYFczasTeraz/CYFczasCaly);

procent = (CYFczasTeraz/CYFczasCaly);
Serial.print(procent,2);
Serial.println('%');

Serial.println((5/10),DEC);
Serial.println((10/2),DEC);
}
}
else
{
if (pomoc >=20)
{
pomoc=0;
Serial.println('*');
}
else
{
pomoc++;
Serial.print('#');
delay (1000);
}
}
}

Serial.println((5.0/10.0));
    Serial.println((10.0/2.0));
procent = (CYFczasTeraz/CYFczasCaly);

Integer divide does not produce fractional results.

Thanks for the advice, but still I don't know how to solve the problem. There isn't a variable type for my case or it is?
If I change the variable procent to float then everything before the divide operation goes wrong and puts wrong results.
If I try to get the rest from sharing '%' I receives wrong results.

What more should I read about?

Re-read #1. Cast to float during the print operation, variables can remain as integers.

Still don't understand what You wrote. :confused: I need examples.

But I have found a solution: multiplication BEFORE dividing:

procent = ((100*CYFczasTeraz)/CYFczasCaly);

11Master:
Still don't understand what You wrote. :confused: I need examples.

But I have found a solution: multiplication BEFORE dividing:

procent = ((100*CYFczasTeraz)/CYFczasCaly);

Still missing the point. CYFczasTeraz and CYFczasCaly are both integers. When the compiler does it's thing, it looks at the right hand side of the equation and chooses a data type to hold the temporary value of the calculation prior to the assignment. In this case, as both are integers it means the result will be held in an integer, which means no fractional parts. Furthermore, procent is also an integer and would never hold fractional parts either.

Firstly, procent must be declared as a float,

float procent =(CYFczasTeraz/CYFczasCaly);

but that's not enough as both on the right are integers still. To force the compiler to use a float for the intermediate calculation, at lrast one of the variables on the right hand side must be (temporarily) cast as a float. So now you have,

float procent =((float)CYFczasTeraz/CYFczasCaly);, or
float procent =(CYFczasTeraz/(float)CYFczasCaly);, or
float procent =((float)CYFczasTeraz/(float)CYFczasCaly);,

any one of which will give the same result and the answer you're looking for.

Thank again for explanation. I understand Your idea but i did not know how to write it with the variables, I didn't know the syntax.
Can You last time help me and give the link with some tutorials with that - "(varuiable type) variable" to change temporary type of them?

Last small question: which solution is better?

I'm guessing Polish as your language (forgive me if I'm wrong)

Thanks for link. You right with polski :slight_smile:

Admin can set this topic as solved. Thanks for dedicated time.

Admin can set this topic as solved.

You can edit the description in your original post and add [SOLVED] to it

11Master:
Thanks for link. You right with polski :slight_smile:

Admin can set this topic as solved. Thanks for dedicated time.

Admins don't do that sort of thing here.