float to char array

I need to convert a float, including negative floats, to a char array.

I have tried a lot of things and can't get anything to work. This is my last effort.

// val is a float

int val_int = (int) val; // compute the integer part of the float

float val_float = (abs(val) - abs(val_int))*100000;

int val_fra = (int)val_float;

sprintf (sz, "%d.%d", val_int, val_fra); //

When the float is -96.12345, I get -96.0

Any help would be appreciated.

Dana

Google for:
Arduino sprint float

You'll see an explanation and some imaginative work arounds.

Thanks.

I have tried all that I can find and when it does (work) it gets the decimal part wrong. I hard coded
-96.123465.

This, taken more or less from your code, on an arduino Uno :

void setup() {

  Serial.begin( 115200 ) ;
  
  float  val = -96.12345 ;  // test value
  
  char sz[20] = {' '} ;

  int val_int = (int) val;   // compute the integer part of the float

  float val_float = (abs(val) - abs(val_int)) * 100000;

  int val_fra = (int)val_float;

  sprintf (sz, "%d.%d", val_int, val_fra); //
  Serial.println ( sz ) ;
}

void loop() {

}

gives this result:

-96.12345

Do you need more precision than that ?

Better use this:

  sprintf (sz, "%d.%05d", val_int, val_fra); //

Otherwise you'll get "0.5" when the value is "0.00005" or "0.0005" or any other similar value.

(Better yet, use the well-tested "ftoa" (correction: "dtostrf") function that is included in the AVR libc.)

Better yet, use the well-tested "ftoa" function that is included in the AVR libc.

Can you provide a link for that. All that I see avr-libc 2.0.0 Standard C library for AVR-GCC is dtostrf() in stdlib.h.

Just use dtostrf(). It works just fine with floats

6v6gt:
This, taken more or less from your code, on an arduino Uno :

void setup() {

Serial.begin( 115200 ) ;
 
  float  val = -96.12345 ;  // test value
 
  char sz[20] = {' '} ;

int val_int = (int) val;  // compute the integer part of the float

float val_float = (abs(val) - abs(val_int)) * 100000;

int val_fra = (int)val_float;

sprintf (sz, "%d.%d", val_int, val_fra); //
  Serial.println ( sz ) ;
}

void loop() {

}





gives this result:

-96.12345


Do you need more precision than that ?

Serial.print will work, but I am not using that. I am using the sz in another function. That function works
because I tested it using strcpy and then sending that string to the function.

cattledog:
Can you provide a link for that. All that I see avr-libc 2.0.0 Standard C library for AVR-GCC is dtostrf() in stdlib.h.

Sorry, you're right. I meant dtostrf(). I was thinking it was called ftoa() because itoa() exists to convert an int to string.

TXEng:
Serial.print will work, but I am not using that. I am using the sz in another function. That function works
because I tested it using strcpy and then sending that string to the function.

In principle, it is always good to provide a minimal program which demonstrates the problem. It seems that your problems may then lie in the later processing if you have succeeded in correctly forming a character array based on the float number.

UKHeliBob:
Just use dtostrf(). It works just fine with floats

Thanks, that seems to work fine.

Dana