Bonjour à tous,
J'ai une fonction que je n'ai pas écrite t qui va afficher la latitude avec 6 chiffres après le point.
printFloat(latitude, 5);
En réalisté il va afficher le nombre de virgule en fonction du deuxième paramètre. Dans cet exemple, il en affichera 5.
Voici la fonction et ma question après:
void printFloat(float value, int places) {
// this is used to cast digits
int digit;
float tens = 0.1;
int tenscount = 0;
int i;
float tempfloat = value;
// make sure we round properly. this could use pow from <math.h>, but doesn't seem worth the import
// if this rounding step isn't here, the value 54.321 prints as 54.3209
// calculate rounding term d: 0.5/pow(10,places)
float d = 0.5;
if (value < 0)
d *= -1.0;
// divide by ten for each decimal place
for (i = 0; i < places; i++)
d/= 10.0;
// this small addition, combined with truncation will round our values properly
tempfloat += d;
// first get value tens to be the large power of ten less than value
// tenscount isn't necessary but it would be useful if you wanted to know after this how many chars the number will take
if (value < 0)
tempfloat *= -1.0;
while ((tens * 10.0) <= tempfloat) {
tens *= 10.0;
tenscount += 1;
}
// write out the negative if needed
if (value < 0)
Serial.print('-');
if (tenscount == 0)
Serial.print(0, DEC);
for (i=0; i< tenscount; i++) {
digit = (int) (tempfloat/tens);
Serial.print(digit, DEC);
tempfloat = tempfloat - ((float)digit * tens);
tens /= 10.0;
}
// if no places after decimal, stop now and return
if (places <= 0)
return;
// otherwise, write the point and continue on
Serial.print('.');
// now write out each decimal place by shifting digits one by one into the ones place and writing the truncated value
for (i = 0; i < places; i++) {
tempfloat *= 10.0;
digit = (int) tempfloat;
Serial.print(digit,DEC);
// once written, subtract off that digit
tempfloat = tempfloat - (float) digit;
}
}
J'utilise un µP 32u4, donc avec un Serial1.print(). Aux pin Tx et Rx j'ai mis un OpenLog et ceci
[code]
float fl = 46.987800;
Serial1.print(F("Ecrire dans ma carte SD le float :"));
Serial1.println(fl, DESC);
Ma premiere question.
Es-ce que
Serial1.println(fl, DEC);
va ecrire la même chose que l'affichage de
Serial.println(fl, DEC);
Qu'es-ce qu'excatement le DEC? Le DEC va forcer l'affichage ou l'écriture en Décimal?
Es-ce que le DEC va me conserver les 5 chiffre après la virgule comme si je faisais
Serial.println(fl, 5);
Merci pour vos lumières!!