Use: lWhole =3D fInput; ulPart =3D ( fInput - lWhole ) * MULTIPLIER; printf( "The floating point number is: %li.%02li\n",lWhole, ulPart ); instead. The "(far char *)" part and some of that typecasts are not strictly necessary. Isaac Em 15/11/2011 20:38, rchadwic escreveu: > I am using MPLab C18 Compiler V3.38 to program an 18C4550; presently tryi= ng > to use the ADC to convert voltage on a potentiometer and send it on a ser= ial > UART) interface to be displayed on Hyperterminal.=20 > I am using the code suggested in > http://support2.microchip....aspx?ID=3DSQ6UJ9A003PQX > > to convert a floating point number (ADC voltage), using printf to send th= e > result to the serial output. Code is reproduced below.=20 > I find that the code does not reproduce the ASCII value correctly if the > floating point number leads with a zero behind the decimal point.=20 > Example: > Floating point number 2.9984 ASCII 2.99 (good) > 3.0018 ASCII 3.18 (bad) > 3.095 ASCII 3.95 (bad) > 3.104 ASCII 3.10 (good) > It looks like the code somehow is dropping leading zeros, but I can not s= ee > how or see a way to make it stop doing this.=20 > Does anyone have any suggestions? Thank you.=20 > > CODE: (copied from Microchip note) > float fInput=3D344.9876; // Pick a number > long lWhole=3D0; // Stores digits left of decimal > unsigned long ulPart=3D0; // Stores digits right of decimal > > #define MULTIPLIER 100 //(use 1 for no decimals, 10 for 1 place, > // 100 for 2 places, etc) > > #include > #include // choose your device as needed > > void main(void) > { > // Set up the USART for output (not required, but since printf sends to t= he > USART, it makes the output readable in the output window) > SPBRG=3D15; > TXSTA=3D0x24; > RCSTA=3D0x90; > > > //Convert number from float to fixed point for display. > //The number is converted to two parts. > lWhole=3D(long)((float)fInput); > ulPart=3D(long)((float)fInput*MULTIPLIER)-lWhole*MULTIPLIER; > > printf((far char *)"The floating point number is: %li.%li\n",lWhole,ulPar= t); > while(1); // wait forever > } > > > --=20 http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist .