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

Leading Zeros in floating point

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



Joined: 19 Jan 2006
Posts: 22

View user's profile Send private message

Leading Zeros in floating point
PostPosted: Sat Mar 18, 2006 3:24 am     Reply with quote

Hi,

I've been using the format modifiers for outputting data recently - for instance putting %3u ensures that when outputting an integer, three digits are always used - padded wit zero's if needed.

Using floats, I have tried both %3.1 and %03.1, but neither give a conistent number of digits - so for instance I get 123.4 or 8.5, rather than 008.5

Is there a way around this (other than just using a switch sttement to see how big the number is and manually insert padding)

Cheers,

Ben
Ttelmah
Guest







PostPosted: Sat Mar 18, 2006 4:05 am     Reply with quote

The key is to understand what the digit before the decimal point 'means'. It is not the number of digits in front of the DP, but the _total_ field width. Hence %03.1, will only guarantee on digit in front of the DP, and if given a number of ten or above, 'overflow', and have to add en extra leading digit. To give three digits in front of the DP, with one digit after, you have to use %05.1 (three digits in front, the DP, and one trailing digit).
It also depends on your compiler version. Until quite recently, the handling of leading zeros on the float formats, was 'faulty'...

Best Wishes
Guest








PostPosted: Sat Mar 18, 2006 6:31 am     Reply with quote

Ah, ok. So %03.1 initially sets the entire width of the outputed number to 3 symbols, of which one is a decimal point, and with one trailing digit.

So we are guaranteed at least x.y

If we have a number larger than 9/99 etc, extra leading digits are inserted. %05.1 guarantees:

wxy. z
12345

That makes sense - I must have misunderstood the manual. What then is the meaning oif neglecting the 0:

%3.1 as opposed to %03.1?

Cheers,

Ben


btw, I have made my compass readout work - a compass pointer is drawn onto a rose on my Nokia 3310 LCD that points in a reasonable approximation to North. The code is quite modular, so I'll post it on the examples page when its looking a bit nicer.
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