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 CCS Technical Support

printf %3.1w leading zeros

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



Joined: 23 Feb 2007
Posts: 55

View user's profile Send private message

printf %3.1w leading zeros
PostPosted: Fri Apr 06, 2007 5:50 am     Reply with quote

Hi!

I need to print an integer in this format: 00.0
I use printf %3.1w and it works but it doesn't print the leading zero. For example:

value = 4.2 -> output = 4.2

I'd like output = 04.2

How should I fix it?

Thanks
Marco / iw2nzm
Ttelmah
Guest







PostPosted: Fri Apr 06, 2007 7:05 am     Reply with quote

Two things. On all the numeric formats, you add a leading 'zero' to the format, to make the extra leading spaces become filled with zeros. However as specified, you have no leading spaces, since your field 'width', matches the output (4.2, is three characters wide).
So you need to use:
printf("%04.1w",val);

To output a four character wide field, with a leading zero.

Best Wishes
iw2nzm



Joined: 23 Feb 2007
Posts: 55

View user's profile Send private message

PostPosted: Fri Apr 06, 2007 8:09 am     Reply with quote

Ttelmah wrote:
Two things. On all the numeric formats, you add a leading 'zero' to the format, to make the extra leading spaces become filled with zeros. However as specified, you have no leading spaces, since your field 'width', matches the output (4.2, is three characters wide).
So you need to use:
printf("%04.1w",val);
Best Wishes


Thank you very much, now it works fine!

Marco / iw2nzm
future



Joined: 14 May 2004
Posts: 330

View user's profile Send private message

PostPosted: Fri Apr 06, 2007 1:10 pm     Reply with quote

I noticed a big speed impact using %4.1w over %u02.%u1 showing the quotient and rest of a division.

Maybe the compiler uses 16/16 in its internal routines.
Ttelmah
Guest







PostPosted: Fri Apr 06, 2007 2:30 pm     Reply with quote

You will notice, that %w, does not need a 'L' specifier. it assumes that you will be using a 'long', since otherwise the range is so limited.

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