 # How can count the lenght of a float and remove the last if too long

Good,
I would like to make sure that a float value is not biger than expected.

Below, the float value will be converted to a char and save to the destination variable.

But we never know how big will be the float (45.0002345045)

``````float number = 45.0002345045;

#define DESTINATIONSIZE 12
char destination[DESTINATIONSIZE]

convertFloatToChar(number,destination,10);

void convertFloatToChar(float source, char * destination, int nbDec)
{
// HERE I WOULD LIKE TO REMOVE THE LASTDIGIT

if(strlen(source) > DESTINATIONSIZE-1)
{
// HERE NUMBER WILL TAKE THE VALUE OF : 45.00023450
// BECAUSE 45 are to much
}

}
``````

this

``````if(strlen(source) > DESTINATIONSIZE-1)
``````

foes not work and I do not know how to count the length of a float

and how can I remove the last digit of a float which exceed the size of DESTINATIONSIZE-1?

Here is all the code of
(I would like to avoid that destination is overload)

``````void convertFloatToChar(float source, char * destination, int nbDec)
{

//if(strlen(source) > DESTINATIONSIZE)
//{
// sprintln(F("ERREUR : Source is greater than Dsitnation"),2);
// Modify
//}

int ndec = nbDec;
String r = "";
int v = (int)source;

r += v;     // whole number part
r += '.';   // decimal point

int i;
for (i=0;i<ndec;i++) {
// iterate through each decimal digit for 0..ndec
source -= v;
source *= 10;
v = source;
r += v;
}

//Convert to char
r.toCharArray(destination,r.length()+1);

for(int y=0; y<strlen(destination); y++)
{
if(destination[y]=='\0') SerialUSB.println(F("NULL"));
}

//SerialUSB.println(destination);

}
``````

A floating point variable doesn't really have a last digit. Remember that floats are approximations. Good approximations, but inexact at best. They're really only good to 6 or 7 digits no matter where the decimal is. So just plan to capture 7 digits plus a decimal point and call it even. Anything more than that are just useless digits.

Hello! Ok but how can I make sure that here

``````r.toCharArray(destination,r.length()+1);
``````

Would it possible to do this

``````int r_len;
if(r.length()+1 >= DESTINATIONSIZE)
{
r_len = DESTINATIONSIZE-1; // To keep a place for '\0'
}
else
{
r_len = r.length()+1;
}

r.toCharArray(destination,r_len);
``````

but if r.length()+1 >= DESTINATIONSIZE, which number will I loose, the first or the last?

Doesn't the String class already have a method for turning a float into a String?

Heu, I would like to convert a float into a char, not a String

A char is a single byte - converting 32 bits of float into 8 bits makes no sense.

AWOL: A char is a single byte - converting 32 bits of float into 8 bits makes no sense.

But I need to use the float variable with a function which requiered a char. It's the reason why, I need to comvert a float into a char

It's the reason why, I need to comvert a float into a char

If you mean a [u]character string[/u], like "12.2345" use dtostrf(). With dtostrf you can specify the maximum length of the string.

I saw r.length which is a String function and that led me to believe you wanted a String.

As Delta_G pointed out, a float data type only has 6 or 7 digits of precision. Everything after that is fluff that doesn't really mean anything significant. However, if you still want to keep 12 digits, why not just:

``````#define FLOATPRECISION  7      // At the top of source file

// Assumes destination contains something like: "45.0002345045"
void convertFloatToChar(float source, char * destination, int nbDec)
{
int len = strlen(destination);

if (len > DESTINATIONSIZE) {
destination[DESTINATIONSIZE] = '\0';
}

}

}
``````

This makes no attempt to round the number, but that could be done. Of course, rounding doesn't mean much if you're rounding digits past a float precision. If destination[] doesn't hold the float representation, you can use dtostrf() as jremington suggested.

45.0002345045 cannot even be represented as a float at all. It will be rounded to either 45.0002 or 45.00023 at best. A float is incapable of giving more the 7 significant digits and often only 6.

Regards, Ray L.

pierrot10: But we never know how big will be the float (45.0002345045)

A 'float' is always 4 bytes big. So what?

If you want to do "float to decimal conversion", the number of digits is "unlimited", but only the first 6-7 significant digits will be correct.

If you cannot live with incorrect 'float' values in decimal conversion, never use incorrect decimal values, which means: Never use 'float'!

In some cases 'rounding' may help (depends on the application).

jremington: If you mean a [u]character string[/u], like "12.2345" use dtostrf(). With dtostrf you can specify the maximum length of the string.

Not quite, if I remember correctly. I'm pretty sure that you can set the maximum number of decimal places, (precision), and the minimum number of characters, (width), but not the maximum. It pads out to the minimum with leading white spaces.

Edit: I just found my tests from a couple of months ago:-

``````void setup()
{
//  dtostrf(val, width, precision, buffer);

Serial.begin(115200);
double dVal = 123.4567;
char buffer;
dtostrf(dVal, 5, 2, buffer);  // Prints "123.46"
Serial.println(buffer);

dtostrf(dVal, 4, 2, buffer);  // Prints "123.46"
Serial.println(buffer);

dtostrf(dVal, 2, 3, buffer);  // Prints "123.457"
Serial.println(buffer);

dtostrf(dVal, 6, 2, buffer);  // Prints "123.46"
Serial.println(buffer);

dtostrf(dVal, 7, 2, buffer);  // Prints " 123.46"
Serial.println(buffer);

dVal=0.12345;
dtostrf(dVal, 0, 2, buffer);  // Prints "0.12"
Serial.println(buffer);
}
``````