# Output decimal serially to user while not being allowed to use floating point???

#### danielb33

Joined Aug 20, 2012
105
I have serial communication set up for a motor controller. I am outputting much data (current, temperature, voltage, etc.) and would like to NOT require the user to think, "Oh yeah, I need to move the decimal two places to the left before writing my answer down." I am working with a stm32F1, lots of processing power, but my boss says not to use floating point during this project.

I KNOW that the decimal will ALWAYS be shifted one, two, or three decimals to the left. Is there a way to make decimal format outputted?

I am using a standard _IO_putchar() function and printf() to output data.

Thanks in advance for any help!

#### thatoneguy

Joined Feb 19, 2009
6,359
You could use a strncpy or similar function to pull the last 1-3 digits out of the output string, insert a decimal point, then print the rest of the digits.

What size is the buffer holding the number? If it is always 2 decimal places, displaying a point can be done without floating point math.

#### ErnieM

Joined Apr 24, 2011
8,346
Do you really need printf()? It's a big function. See if you have the itoa() function, for Int to ASCII (ItoA)... then you have a string you can insert the decimal point in.

If you scale your numbers correctly you can avoid using a float. Example: Say you do your computations in millivolts... then tag the DP where it need be.

#### danielb33

Joined Aug 20, 2012
105
ThatoneGuy- Not sure how to use strcpy. I am using printf with _io_putchar so every time I send a character via printf(), that character is automatically stored in a buffer via hardware to be transmitted thru an interrupt. The converted values I am sending thru printf() are ints, so I don't think strcpy will work.

ErnieM - itoa() does not work with my micro.

Anybody- Any other ways to convert integers to strings???? This is a good idea. If I can do this I can insert the decimal and use printf().

#### Ian Rogers

Joined Dec 12, 2012
1,028
I did my own....
Rich (BB code):
void printFloat(char * flt, long number, char digits)
{
if(number < 0)
{
number = ABS(number);
}
if(digits == 2)
{
flt[0] = number / 1000 + 48;
if(flt[0] == 48) flt[0] = 0x20;
flt[1] = ((number % 1000) / 100)+ 48;
flt[2] = 46;
flt[3] = ((number % 100) / 10)+ 48;
flt[4] = (number % 10) + 48;
flt[5] = 0;
}
else
{
flt[0] = number / 100 + 48;
if(flt[0] == 48) flt[0] = 0x20;
flt[1] = ((number % 100) / 10)+ 48;
flt[2] = 46;
flt[3] = (number % 10) + 48;
flt[4] = 0;
}
}
I needed one or two decimal places with 5 overall digits..... It works for me..

PS you need an ABS() function if any numbers is negative..

#### ErnieM

Joined Apr 24, 2011
8,346
itoa() sometimes lives in stdlib.h, which is a strange place to find it as it's NOT part of the C standard. If your lib doesn't have it you can always google a version.

#### t06afre

Joined May 11, 2009
5,934
Also remember that you do have to convert the data to string before sending it down the serial link. At least as long as you also control the software in the receiving end. And this is running on a standard PC.

#### thatoneguy

Joined Feb 19, 2009
6,359
If you are only dealing with one byte at a time and putchar, what order are you sending the data? L->R or R->L?

Count the number of characters sent, if it is equal to x, hold current byte and send decimal point, then current byte, and continue sending the rest.