Go Down

### Topic: Convert a Float to an Array (Read 1 time)previous topic - next topic

#### TheBookworm

##### Feb 04, 2013, 04:41 am
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?

#### lloyddean

#1
##### Feb 04, 2013, 04:47 amLast Edit: Feb 04, 2013, 06:19 am by lloyddean Reason: 1

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

#### holmes4

#2
##### Feb 04, 2013, 04:51 am
What do you mean by process it?

Mark

#### michinyon

#3
##### Feb 04, 2013, 04:57 am
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 ?

#### TheBookworm

#4
##### Feb 04, 2013, 04:58 am

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.

#### Arrch

#5
##### Feb 04, 2013, 04:59 am
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.

#### TheBookworm

#6
##### Feb 04, 2013, 05:04 am

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

#### GoForSmoke

#7
##### Feb 04, 2013, 05:37 am
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.

2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

#### michinyon

#8
##### Feb 04, 2013, 06:30 am
Code: [Select]
`float f=43.24char 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

#9
##### Feb 04, 2013, 11:28 am
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.