CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

floats and printf()

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Pere Riu
Guest







floats and printf()
PostPosted: Wed Oct 30, 2002 7:52 am     Reply with quote

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()
PostPosted: Wed Oct 30, 2002 12:01 pm     Reply with quote

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()
PostPosted: Wed Oct 30, 2002 3:47 pm     Reply with quote

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()
PostPosted: Sat May 23, 2009 6:19 am     Reply with quote

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







PostPosted: Sun May 24, 2009 2:34 am     Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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