# Help with modulo. 25%10 always rounds off to 6.

void processNumber(int number)
{
int x;
unsigned int ones;
int tens;

ones=(number)%10;
x=(number)-ones;
tens=x/10;

updateDisplay(ones,tens);
}

here is my sample code that tries to display on 2 digit 7 segment timer.
every time the number has a value of 5 at the end. example 25 it always rounds off to 26
any suggestion amazing people?

Thanks in advance! How to use this forum - please read.

1. If you are posting code or error messages, use "code" tags
1. Tips for getting the most out of your post
Post a complete sketch (program code)! If you don't you waste time while people ask you to do that. However, with coding problems, if possible post a "minimal" sketch that demonstrates the problem - not hundreds of lines of code. If the problem goes away in the minimal sketch, it wasn't where you thought it was.

You're dealing with decimals not int. try float .

Your code works for me and, although it is a bit cumbersome, I don't see how it could produce 26 from 25. It should work for any integer from 0 to 99.
Try this (and notice the code tags):

``````void processNumber(int number)
{
updateDisplay(number % 10,number/10);
}
``````

Pete

works for me, too:
output is exactly what you intended

``````int ones, tens;

void setup() {
// put your setup code here, to run once:
Serial.begin(115200);

}

void loop() {
// put your main code here, to run repeatedly:
for (int i=0; i<100; i++){
processNumber(i);
Serial.print(i);
Serial.print("\t");
Serial.print(tens);
Serial.print("\t");
Serial.println(ones);
}
while (true){};
}

void processNumber(int number)
{
int x;
// unsigned int ones;
// int tens;

ones=(number)%10;
x=(number)-ones;
tens=x/10;

// updateDisplay(ones,tens);
}
``````

output:

``````0	0	0
1	0	1
2	0	2
3	0	3
4	0	4
5	0	5
6	0	6
7	0	7
8	0	8
9	0	9
10	1	0
11	1	1
12	1	2
....
88	8	8
89	8	9
90	9	0
91	9	1
92	9	2
93	9	3
94	9	4
95	9	5
96	9	6
97	9	7
98	9	8
99	9	9
``````

Can you post your code of updateDisplay(x, y); ?

erdiegange:

``````void processNumber(int number)
``````

{
int x;
unsigned int ones;
int tens;
ones=(number)%10;
x=(number)-ones;
tens=x/10;
updateDisplay(ones,tens);
}

I don’t know if it’s what’s causing your problem but you don’t need to round (number) before dividing it by 10 as tens is an int and can’t hold the fractional part of the result.

``````   ones=number%10;
tens=number/10;
``````

Also, why the brackets around number?
You are calling a function from within a function. I seem to remember reading that that’s a no-no. :~

No that's ok, you can call functions from anywhere, loop() is already called from main() so every time you do (for example) a Serial.print() inside loop() you are already calling a function from inside a function, and for all you know Serial.print() calls 10 other functions all of which call other functions again.

Subject to the stack overflowing there is no limit.

It seems that erdiegange's code works for everyone else, so the only unknown is the updateDisplay() function.

Rob

You wrote:

"You are calling a function from within a function. I seem to remember reading that that's a no-no."

As others have pointed out, this is perfectly OK. The stack size varies, as does the amount of stack used (depends upon number and type of parameters for each function, as well as local variables used in each function), so it is challenging to give a hard limit on this.

What you may be remembering is that C / C++ does not permit the DEFINITION of a function within a function.