|
|
View previous topic :: View next topic |
Author |
Message |
Pere Riu Guest
|
floats and printf() |
Posted: Wed Oct 30, 2002 7:52 am |
|
|
HI,
I am using printf so display floating point numbers in a LCD:
printf(lcd_putc,"\%05.2f",myfloat);
but I never get leading "0" or leading white spaces to fill up the requested 5 characters. i.e. if the number is 5.32 I get
"5.32" instead of "005.32" or " 5.32".
Any ideas ?.
Pere
___________________________
This message was ported from CCS's old forum
Original Post ID: 8292 |
|
|
nilsener Guest
|
Re: floats and printf() |
Posted: Wed Oct 30, 2002 12:01 pm |
|
|
Dear,
suggestion:
1. convert the float to a string
sprintf(string, "\%5.2f", myfloat); //convert float to string
// string should be '5.32'
2. find the position of '.' in the string
ptr = &string[0]; //set pointer to beginning of string
ptr1 = strchr(string,'.'); //find '.' in string
k = ptr1 - ptr; //calculate absolute position of '.'
// k should be 1 in this case;
3. build a second string with the leading zeros
if (k == 1) strcpy(string2,"0000");
if (k == 2) strcpy(string2,"000");
if (k == 3) strcpy(string2,"00");
...
...
3. concatenate string ('5.32') onto string2 ('0000')
strcat (string2, string);
now string2 should contain '00005.32'
printf(lcd_putc,string2);
Maybe it works ?
regards nilsener
:=HI,
:=
:=I am using printf so display floating point numbers in a LCD:
:=
:=printf(lcd_putc,"\%05.2f",myfloat);
:=
:=but I never get leading "0" or leading white spaces to fill up the requested 5 characters. i.e. if the number is 5.32 I get
:="5.32" instead of "005.32" or " 5.32".
:=Any ideas ?.
:=
:=Pere
___________________________
This message was ported from CCS's old forum
Original Post ID: 8305 |
|
|
pere riu Guest
|
Re: floats and printf() |
Posted: Wed Oct 30, 2002 3:47 pm |
|
|
Thanks ! That's what I am doing right now. Simply I was expecting something more clever from the compiler side.
Pere
:=Dear,
:=
:=suggestion:
:=
:=1. convert the float to a string
:=
:=sprintf(string, "\%5.2f", myfloat); //convert float to string
:=
:= // string should be '5.32'
:=
:=2. find the position of '.' in the string
:=
:=ptr = &string[0]; //set pointer to beginning of string
:=
:=ptr1 = strchr(string,'.'); //find '.' in string
:=k = ptr1 - ptr; //calculate absolute position of '.'
:=
:= // k should be 1 in this case;
:=
:=3. build a second string with the leading zeros
:=
:=if (k == 1) strcpy(string2,"0000");
:=if (k == 2) strcpy(string2,"000");
:=if (k == 3) strcpy(string2,"00");
:=...
:=...
:=
:=3. concatenate string ('5.32') onto string2 ('0000')
:=
:=strcat (string2, string);
:=
:=now string2 should contain '00005.32'
:=
:=printf(lcd_putc,string2);
:=
:=Maybe it works ?
:=
:=regards nilsener
:=
:=
:=:=HI,
:=:=
:=:=I am using printf so display floating point numbers in a LCD:
:=:=
:=:=printf(lcd_putc,"\%05.2f",myfloat);
:=:=
:=:=but I never get leading "0" or leading white spaces to fill up the requested 5 characters. i.e. if the number is 5.32 I get
:=:="5.32" instead of "005.32" or " 5.32".
:=:=Any ideas ?.
:=:=
:=:=Pere
___________________________
This message was ported from CCS's old forum
Original Post ID: 8316 |
|
|
PaulF Guest
|
Re: floats and printf() |
Posted: Sat May 23, 2009 6:19 am |
|
|
Pere Riu wrote: |
I am using printf so display floating point numbers but I never get leading "0"
printf("%5.2f",myfloat);
|
I have the same problem, my workaround is
printf("%05s", sprintf("%.2f",myfloat)); |
|
|
Ttelmah Guest
|
|
Posted: Sun May 24, 2009 2:34 am |
|
|
The 'reason', is that the field length being specified is too short for the required leading zeros to be displayed.
In C, the field length (the bit on the left of the decimal), is for the _entire_ output field. This includes the decimal point, the trailing digits, and the leading location for the sign. To display three digits in front of the decimal, with two digits after, you would need to use a field specifier of %07.2f.
Best Wishes |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|