Go Down

Topic: Convert a Float to an Array (Read 576 times) previous topic - next topic

Michael Bell

So as part of a project I am working on, I need to convert float numbers (ie 42.24 or 123.45) into an array like so:













|0|4|2|2|4|






|1|2|3|4|5|




This way I can access the first digit, process it, access the second digit, process it, etc, etc...

Can anyone provide me with the way to do this?
Hi!

lloyddean

#1
Feb 04, 2013, 04:47 am Last Edit: Feb 04, 2013, 06:19 am by lloyddean Reason: 1
Use Google to lookup 'sprintf'

EDIT: Boy did I mess that one up.
NOTE TO SELF: Ignore the urge to post from a mobile device!

holmes4

What do you mean by process it?

Mark

michinyon

Do each of those little boxes represent integers or chars ?

Are the numbers always the same length ?

How do you know where the decimal point goes ?

Michael Bell


What do you mean by process it?

Mark



Do something with it. What the program eventually will be doing is taking each digit from the program and pulsing the number on a speaker as part of a larger setup, so for 45.32 it would pulse 4 times, take a break, pulse 5 times, 3 times then 2 times.
Hi!

Arrch

Multiply it by 10^n where n is the precision, assign the result to an int, and pass it into itoa()

That will give you a char representation. If you need the numeric value, subtract '0' from the digit.

Michael Bell


Do each of those little boxes represent integers or chars ?

Are the numbers always the same length ?

How do you know where the decimal point goes ?


Each box represents a char, the numbers are all in the format 000.00, they will never get larger, so the number could be 000.01 or it could be 018.00, and I would write it out like 0|0|0|0|1 or 0|1|8|0|0. It is my way of showing you the array, so for the array for 123.34 would be manually created like this:
Code: [Select]
int array[] = {
  1, 2, 3, 4,  }; 



I apologize if this isn't making sense, I have trouble explaining my thoughts
Hi!

GoForSmoke

OP: You could use sprint() to turn the float into a C string and process the text or you could multiply the float by 1000 if you want to save to 3 places and store the value in a long and chop through it using more direct integer techniques, the kind you did with pencil on paper in the old days. The least digit is the remainder of divide by 10, the modulo (%) operation gives that, a loop can chop off the low digit of the number every time around until there's nothing left. That's right to left, can be buffered for later left to right read or save or activate order.

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

michinyon

Code: [Select]

float f=43.24
char buf[7] ;
sprintf(buf,"%f",f);
char result[6] ;
int j=0 ;
for ( int i=0 ; i<7 ; i++ )
{
    char ch = buf[i] ;
    if ( '0' <= ch && ch <= '9' )
    {
       result[j]= ch ;
       j++;
    }
}
result[j]='\0' ;




Something like this will probably work.

PaulS

Quote
Something like this will probably work.

Or not, since the %f format specifier is not supported on the Arduino.

@OP
Look at the dtsostf() function. It IS supported on the Arduino.

Go Up