Pages: [1]   Go Down
Author Topic: Convert a Float to an Array  (Read 507 times)
0 Members and 1 Guest are viewing this topic.
British Columbia, Canada
Offline Offline
Newbie
*
Karma: 0
Posts: 4
I like to learn new things
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Hi!

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
« Last Edit: February 04, 2013, 12:19:24 am by lloyddean » Logged

Poole, Dorset, UK
Offline Offline
Edison Member
*
Karma: 50
Posts: 2207
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What do you mean by process it?

Mark
Logged

Offline Offline
Faraday Member
**
Karma: 61
Posts: 2874
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 ?
Logged

British Columbia, Canada
Offline Offline
Newbie
*
Karma: 0
Posts: 4
I like to learn new things
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Hi!

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3368
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

British Columbia, Canada
Offline Offline
Newbie
*
Karma: 0
Posts: 4
I like to learn new things
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
int array[] = {
  1, 2, 3, 4,  }; 


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

Hi!

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 96
Posts: 4773
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Logged

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

Offline Offline
Faraday Member
**
Karma: 61
Posts: 2874
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1]   Go Up
Jump to: