writefln question

Regan Heath regan at netwin.co.nz
Fri Mar 17 16:51:00 PST 2006


On Fri, 17 Mar 2006 23:59:45 +0000 (UTC), pmoore  
<pmoore_member at pathlink.com> wrote:
> I can't give a number which is < 10 a precision. Is there a good reason  
> for this
> or is this a bug?
>
> eg.
>
> writefln("number = %.8x", cast(ulong)10);
>
> will display
>
> 0000000a
>
> but
>
> writefln("number = %.8x", cast(ulong)9);
>
> will display
>
> 9
>
> Why is the precision ignored in this case?
>
> printf does not work this way. It will correctly display 00000009

Odd. Either it's treating it like a string instead of a number (from the  
printf docs):

For numbers - "The precision specifies the minimum number of digits to be  
printed. If the number of digits in the argument is less than precision,  
the output value is padded on the left with zeros. The value is not  
truncated when the number of digits exceeds precision."

For strings - "The precision specifies the maximum number of characters to  
be printed. Characters in excess of precision are not printed."

Or perhaps, and this is a complete guess, it's related to the way toString  
handles numbers smaller than 10, eg:

if (u < 10)
   // Avoid storage allocation for simple stuff
   result = digits[u .. u + 1];

it takes a slice of a const array:
   const char[10] digits    = "0123456789";			/// 0..9


FYI, you can use:
   writefln("number = %.8x", cast(ulong)9);

to get the result you want. This is how I typically pad my hex numbers.

Regan



More information about the Digitalmars-d-learn mailing list