Separating digits in a number

Hi, i wanted to know if there's a function of some sort that would act like bitRead() on binary numbers, but for decimal numbers: so if i had x=123,4 it would be able to separate each digit from the number I know i could do some division here and there, but would anyone have a more efficient method? I basically need it to then output it to a multiplexed 7-segment led

Print the number to a string and access the digits in the string?

I know i could do some division here and there, but would anyone have a more efficient method?

Nope. Division and modular division is the most efficient way. I don’t beleive the avrs have division instructions, but trying to convert it to a string would be doing the same thing you would be doing except with an added addition (1 + ‘0’ = ‘1’). You would then have to subtract the ‘0’ back out of it, making it less efficient.

if you want performance dive into this thread - -

I made a short program to do just this. It only works with ints the way it is set up now…but with some tweaking it could be used with doubles and floats. I think I commented it fully, but if you have any questions about how it works feel free to ask :).

Here is the code:

 const int ARRAY_LENGTH = 15;  //just to make sure there is enough space (overkill)
 int separatedNum[ARRAY_LENGTH];  //the array to fill with parts of the int
 int actualLength = 0;

void setup() {
  Serial.begin(9600);  //start new serial
  int theNums;  //for some reason I couldn't directly assign the int a value, so I did it in the next line
  theNums = 1234;  //the number we want to split
  separateInt(theNums);  //call the separateInt method
  for(int i = 0; i < actualLength; i++) {
     Serial.println(separatedNum[i]);  //print to serial to display results

void loop() {
  //I just wanted it to run once so this is empty

void separateInt(int theInt) {
  String theString = String(theInt);  //To get length
  actualLength = theString.length();
  int subtract = 0;  //what to subtract from the processed int
  int temp;  //int to store the new processed int

  for(int i = 0; i < actualLength; i++) {
    temp = (theInt/(pow(10, (theString.length()-i-1))))-(10*subtract);  //process the int and subract the previous 10s, 100s, etc...
    subtract = (10*subtract)+temp;  //update what to subtract
     separatedNum[i] = temp;  //add the resulting number to our global int array

Thanks for the code, I see you’ve used some strings in it too. If there isnt any other efficient way, i’ll go by dividing it out to split each number. Thanks anyways

That will probably work, but it is remarkably inefficient.

Hi , can you explain the project you have and how it is connected to the arduino, if it is a multidigit 7seg display and you want to display a number on it like 1234, then there is probably a library and circuit config that will save all the software manipulation.

A CAD or picture in jpg, png or pfd of your circuit would help too.

Tom...... :)

You do not need the strings if you know the exact length of the number you want to split. I only use the strings to get the length of the number.

You have other methods to split binary numbers, because the numbers are stored in binary form in the processor and are easy ways to get those bit from the byte. A decimal number don't have a great meaning for the processor, only to humans because we have 10 fingers in our hands.

for the length of a number you can use the following code

void setup() 
  Serial.println("Start ");
  Serial.println(numlen(1023, 10));

void loop() 

int numlen(long number, int base)
  int cnt = 0;
  if (number < 0)  // count the sign bit as one place
    number = abs(number);
  cnt += (log(number)/log(base)+1); 
  return cnt;