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
how can i display one number after point float number
-
- Posts: 54
- Joined: 03 Apr 2017 09:17
how can i display one number after point float number
- Attachments
-
- Encoder.rar
- (68.29 KiB) Downloaded 86 times
-
- Posts: 1550
- 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
hello,
you can manage a more easy function
or
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);
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);
-
- Posts: 54
- Joined: 03 Apr 2017 09:17
Re: how can i display one number after point float number
thank you o much
it is working good now
it is working good now