Pages: [1]   Go Down
Author Topic: ITOA PROBLEM  (Read 1339 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Edison Member
*
Karma: 16
Posts: 1579
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi!

I`ve some problem with itoa command. I´m using this code to show a float number (with 2 decimals) into a LCD:

char temp[12];
lcd.printIn(itoa((long)fx,temp,10));
lcd.printIn(".");
lcd.printIn(itoa(abs(fx-(long)fx)*100,temp,10));


fx is a float number. I acquire a signal and convert to the appropiate physic value.

The problem is with numbers between 0 and -1. It shows positive, and after you decrease for -1, the sign "-" is showed.

Any idea? :-/


Thank-u


Igor R.



Logged


London
Offline Offline
Tesla Member
***
Karma: 10
Posts: 6250
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Igor,

add code to check if the float is negative, if so print  a minus sign to the LCD and convert the float value to a positive number,  then use your exisitng code.
Logged

Connecticut, US
Offline Offline
Edison Member
*
Karma: 2
Posts: 1036
Whatduino
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

mem, I think that approach will fail for values like 2.045.  It would skip the zero and print "2.5", which is quite different.

May I remind you of an earlier post of your own, http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1207226548/1#1 where you give a suitable lightweight replacement to libc ftoa().  Igor, read down that thread, as the conversation covered this very issue and more.
« Last Edit: December 17, 2008, 01:03:27 pm by halley » Logged

0
Offline Offline
Edison Member
*
Karma: 16
Posts: 1579
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Here you have code (mV to Temp in ºC for a NTC)

Here you have some code:

analog5=analogRead(5);
fanalog5=(analog5)*(5000.0/1024);
lcd.printIn(itoa((long)fanalog5,temp,10));
float fbar;

// APROXIMACIÓN POLINOMIAL ORDEN 3
fanalog5=fanalog5/1000;
fbar=(fanalog5)*(fanalog5)*(fanalog5)*(-4.842)+(fanalog5)*(fanalog5)*(39.692)-(fanalog5)*(122.44)+171.18;
lcd.cursorTo(2,6);
lcd.printIn(itoa((long)fbar,temp,10));
lcd.printIn(".");
lcd.printIn(itoa(abs(fbar-(long)fbar)*100,temp,10));
lcd.printIn("    ");





Logged


London
Offline Offline
Tesla Member
***
Karma: 10
Posts: 6250
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

My lightweight replacement is heaver than Igor needs, but its what I use and it does work! My post above just addresses the sign problem
« Last Edit: December 17, 2008, 01:07:42 pm by mem » Logged

0
Offline Offline
Edison Member
*
Karma: 16
Posts: 1579
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

The problem is that works in all range good, except between 0 to -1.

When the value is -0.456, you can see in the LCD 0.456.

I can modify my code to show a "-", but it´s strange, because after you decrease of -1, it works well (ex: -1.02,-34.45,....)


Logged


London
Offline Offline
Tesla Member
***
Karma: 10
Posts: 6250
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I was suggesting this:
Code:
if( fbar < 0.0){
    lcd.printIn('-');
    fbar = -fbar;
}
lcd.printIn(itoa((long)fbar,temp,10));
lcd.printIn(".");
lcd.printIn(itoa(abs(fbar-(long)fbar)*100,temp,10));
lcd.printIn("    ");

but it only works of there are no zeros after the decimal point.
as halley points out, I posted working code for this here:http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1207226548/1#1
Logged

0
Offline Offline
Edison Member
*
Karma: 16
Posts: 1579
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank-u to both!!!   smiley

I´m going to change all itoa function in my code.....bufff!! je,je,je

Of course, "2,05" is quite different to "2,5"..... and imagine if you are doing a tool to show sensors values....je,je,je   smiley-wink

Logged


0
Offline Offline
Edison Member
*
Karma: 16
Posts: 1579
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

Thinking how I can resolve my problem, without change a lot my code, I´ve have done this. With this I resolve "-" problem, and "0" because I always use only two decimals. It works!!


//... previous code

float fbar;
// APROXIMACIÓN POLINOMIAL ORDEN 3
fanalog5=fanalog5/1000;
fbar=(fanalog5)*(fanalog5)*(fanalog5)*(-4.842)+(fanalog5)*(fanalog5)*(39.692)-(fanalog5)*(122.44)+171.18;
  

lcd.cursorTo(2,6);

if( fbar < 0.0){
    lcd.printIn("-");
    fbar = -fbar;
}

lcd.printIn(itoa((long)fbar,temp,10));
lcd.printIn(".");
fbar=abs(fbar-(long)fbar)*100;
if (fbar<10.0)  lcd.printIn("0");
lcd.printIn(itoa(fbar,temp,10));
lcd.printIn("    ");

//...code


Logged


Connecticut, US
Offline Offline
Edison Member
*
Karma: 2
Posts: 1036
Whatduino
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes, Igor, this is a fine solution for your current needs, although a bit limited and inflexible.  If you wanted three decimals it would be another "if x < 100 print a zero" statement.  Glad you worked this out.
Logged

0
Offline Offline
Edison Member
*
Karma: 16
Posts: 1579
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Of course, I´m agree but  it was a quickly solution....
I´ve added in my TODO list.....je,je,je  smiley-grin

Thank-u!   smiley-wink
Logged


Pages: [1]   Go Up
Jump to: