how can i display one number after point float number

GLCD Font Creator software is the ultimate solution to create personalized fonts, symbols and icons for Graphic LCDs. It generates code compatible with All mikroElektronika compilers
Post Reply
Author
Message
ali genidy
Posts: 54
Joined: 03 Apr 2017 09:17

how can i display one number after point float number

#1 Post by ali genidy » 16 Jan 2021 02:03

hi dear
i wrote a code to read encoder that encoder measure a distance cm and mm
i success to do that but my issue is many many of numbers appears after point i used (FloatToStr_FixLen) now it appear only 3 numbers after point
i want only one number after point like ( 000.0 )
so please can you help me to do that ?
here is my code
Attachments
Encoder.rar
(68.29 KiB) Downloaded 82 times

paulfjujo
Posts: 1544
Joined: 24 Jun 2007 19:27
Location: 01800 St Maurice de Gourdans France
Contact:

Re: how can i display one number after point float number

#2 Post by paulfjujo » 17 Jan 2021 09:19

hello,



you can manage a more easy function

Code: Select all

char Result[6];

void Value_converter(unsigned int adc_value, char *T)
{ 
  long vout ;
  unsigned int value;
    vout= (long)adc_value * 5000 >>10;
    value = (unsigned int) vout;
       T[0] =  value/1000+48;   //Add 48 to get the ASCII character
       T[1]='.' ;
       T[2] = ( value/100)%10+48;
       T[3] =  (value/10)%10 +48;
       T[4] = ( value %10) +48;
       T[5]=0;
}

k=ADC_Read();
 Value_converter(k,Result);
LCD_Out(Result);

or

Code: Select all

char CRam[20];
float F1;


void Float2Ascii (float x, unsigned char *str,char precision)
{
 // converts a floating point number to an ascii string
 // version limitée à 5 decimales maximum
 // x is stored into str, which should be at least 30 chars long
 int ie, i, k, ndig;
 double y;
 if (precision>=5) precision=5;  else precision++;
 ndig =   precision;

 ie = 0;
 // if x negative, write minus and reverse
 if ( x < 0.00000)
 {
   *str++ = '-';
   x = -x;
 }
 // put x in range 1 <= x < 10
 if (x > 0.000000) while (x < 1.000000)
 {
   x *= 10.000;                // a la place de =*
   ie--;
 }
 while (x >= 10.0000)
 {
   x = x/10.0000;
   ie++;
 }
 // in f format, number of digits is related to size
 ndig += ie;                                // a la place de =+
 //round. x is between 1 and 10 and ndig will be printed to
 // right of decimal point so rounding is ...
 for (y = i = 1; i < ndig; i++)
 y = y/10.0000;
 x += y/2.0000;
 if (x >= 10.0000) {x = 1.0000; ie++;}
 if (ie<0)
 {
   *str++ = '0'; *str++ = '.';
   if (ndig < 0) ie = ie-ndig;
   for (i = -1; i > ie; i--)  *str++ = '0';
 }
 for (i=0; i < ndig; i++)
 {
   k = x;
   *str++ = k + '0';
   if (i ==  ie ) *str++ = '.';
   x -= (y=k);
   x *= 10.0000;
  }
 *str = '\0';
}


k=ADC_Read();
F1=(float)K * 5000.0/1024.0;
 Float2Ascii (F1,CRam,1);  // 1 digit after point
 LCD_Out(CRam);


ali genidy
Posts: 54
Joined: 03 Apr 2017 09:17

Re: how can i display one number after point float number

#3 Post by ali genidy » 28 Jan 2021 01:22

thank you o much
it is working good now

Post Reply

Return to “GLCD Font Creator Software”