ITOA PROBLEM

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.

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.

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.

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(" ");

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

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,....)

I was suggesting this:

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

Thank-u to both!!! :slight_smile:

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 :wink:

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

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.

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

Thank-u! :wink: