|
|
View previous topic :: View next topic |
Author |
Message |
barkerben
Joined: 19 Jan 2006 Posts: 22
|
Leading Zeros in floating point |
Posted: Sat Mar 18, 2006 3:24 am |
|
|
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
|
|
Posted: Sat Mar 18, 2006 4:05 am |
|
|
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
|
|
Posted: Sat Mar 18, 2006 6:31 am |
|
|
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. |
|
|
|
|
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
|