[SOLVED]need to convert a int to char array with a decimal point

Hello folks,

I need to convert 7 digit long int xxxxxxx become string "xxx.xxxx" and send the string over serial.print. Converting the long to float : float reg=xxxxxxx/10000.0 than send it with serial.println(reg,4) Obviously it works, but i guess this is not the most effective (both in speed or size) since it introduce (unnecessary?) floating point operation. any thoughts ?

Regards

Divide by 10000 to get the number before the decimal dot; use the modulo 10000 (the mod operator '%') to get the part after the decimal dot.

Can't test but maybe something like below will work

long value
char txBuffer[10];

sprintf("%03ld.%04ld", value / 10000L, value % 10000L);
Serial.println(txBuffer);

Note that you must guarantee that the number will fit in the buffer; 7 digits plus dot plus optional sign plus terminating nul character makes 10.

sterretje:
Divide by 10000 to get the number before the decimal dot; use the modulo 10000 (the mod operator '%') to get the part after the decimal dot.

problem with modulo operator is how to pad with '0' if remaining/fraction part is started with digit 0.
example if the fraction is .0125, it will give .1250.
But i will try it..

Thanks Sterretje !

ubin10:
problem with modulo operator is how to pad with '0' if remaining/fraction part is started with digit 0.

This is taken care of by the sprintf format "%03ld.%04ld". The highlighted part inserts leading zeros.

Whandall:
This is taken care of by the sprintf format “%03ld.%04ld”. The highlighted part inserts leading zeros.

Duh …of course! i didnt notice %04 before
Great…Thx Whandall

sterretje:
Can't test but maybe something like below will work

I tested last night, beautiful !