Try to convert float to character array without leading whitespaces

Hi,

So I am sending some data up to thingspeak. And I noticed that thingspeaks averaging function sometimes didnt work. In a nutshell the data I am sending in the url is ending up with leading whitespaces. The field needs to be long enough to accomodate a value of -40.0 to 100.00

The problem only occurs when my values get down to single digits. So whereas I think I am sending

"2.0" I am actually sending " 2.0" and thingspeak interprets it as a string and doesnt average on it. As its a character array, I can't use trim. I cant for the life of me figure out how to trim whitespaces out of this?

float PanelTemp
float PoolTemp
char URL[230]="";

                strcat(URL,"&field1=");      
                strcat(URL,dtostrf(PanelTemp,4,1,TMPVar));
                strcat(URL,"&field2=");                                                                                  
                strcat(URL,dtostrf(PoolTemp,4,1,TMPVar));  decimal place
                strcat(URL,"&field3=");

You have set the width to '4', so that is what you get.
You can do some string manipulation, to remove the space.

If you set the width to a negative value, the string will be left aligned. That makes it easier to replace any trailing spaces with a zero-terminator '\0'.

The problem is that dtostrf() always formats to the width specification, which you've set to 4. You need to check the temp before you format it to see the width that it needs without padding. If your required space exceeds the width field (e.g., -40.0 uses 5 characters), it expands beyond that field number. However, if you need less than specified (e.g., 9.0), if right justifies the number in the field.

Thanks both. I believe width is a minimum width. So ive set width to 0 on the basis it will expand as needed. I'll test it when I get home

I know this is an old post but I found it via google(first result) and it has no answer... also I could not find anywhere in any manual/docs(avr/arduino) that states:

dtostrf width is indeed a minimum width... great for justifying on a LCD not so great to send to a website :stuck_out_tongue:

so just set it to 0 if you don't want leading spaces on any numbers also it counts the decimal as a space

an example(what I used to test this):

float hello = 22.444;
float world = 2.1;
char Weather[2][8];

void setup() {
  Serial.begin(9600);
  delay(200);
  
  Serial.println("Width = 6, #'s after decimal = 0");
  dtostrf(hello,6,0,Weather[0]);
  Serial.print("'");
  Serial.print(Weather[0]);
  Serial.println("'");

  dtostrf(world,6,0,Weather[1]);
  Serial.print("'");
  Serial.print(Weather[1]);
  Serial.println("'");

  Serial.println();
  Serial.println("Width = 0, #'s after decimal = 2");
  dtostrf(hello,0,2,Weather[0]);
  Serial.print("'");
  Serial.print(Weather[0]);
  Serial.println("'");

  dtostrf(world,0,2,Weather[1]);
  Serial.print("'");
  Serial.print(Weather[1]);
  Serial.println("'");

  Serial.println();
  Serial.println("Width = 3, #'s after decimal = 1");
  dtostrf(hello,3,1,Weather[0]);
  Serial.print("'");
  Serial.print(Weather[0]);
  Serial.println("'");

  dtostrf(world,3,1,Weather[1]);
  Serial.print("'");
  Serial.print(Weather[1]);
  Serial.println("'");

  Serial.println();
  Serial.println("Width = -6, #'s after decimal = 1");
  dtostrf(hello,-6,1,Weather[0]);
  Serial.print("'");
  Serial.print(Weather[0]);
  Serial.println("'");

  dtostrf(world,-6,1,Weather[1]);
  Serial.print("'");
  Serial.print(Weather[1]);
  Serial.println("'");
}

void loop() {
}

and the Serial Output:

Width = 6, #'s after decimal = 0
'    22'
'     2'

Width = 0, #'s after decimal = 2
'22.44'
'2.10'

Width = 3, #'s after decimal = 1
'22.4'
'2.1'

Width = -6, #'s after decimal = 1
'22.4  '
'2.1   '

Edit for typo cause people are dicks

Just do something like this:

char num[8] = " \t2.0";

char *cptr = &num[0];

while (iswhitespace(cptr) cptr++;

// now cptr points to the first non-whitespace character.
// use it instead of num.

Your comment about "trailing zeros" is complete nonsense. Third argument does NOT control the number of trailing zeros.