Re: Zero padding in g_strdup_printf



On Sat, 2 Feb 2008, Kevin DeKorte wrote:

Hi All,

I'm doing a command like this

 *length = g_strdup_printf("%02i:%02.1f", min, seconds);

And if min or seconds is < 10 then I don't get the leading zero 
like I would get in a printf. Is this a known bug.

Works for me.

allin myrtle:~$ pkg-config --modversion glib-2.0
2.14.1

if min is 8 and sec is 5.1 I would expect length to be "08:05.1", 

Then you want the seconds to be printed to a width of 4, not 2.

allin myrtle:~$ cat pad.c 
#include <glib.h>
#include <stdio.h>

int main (void)
{
    gint min = 8;
    gdouble seconds = 5.1;
    gchar *test;

    test = g_strdup_printf("%02i:%04.1f", min, seconds);
    printf("test    = '%s'\n", test);
    g_free(test);
    printf("C native: '%02i:%04.1f'\n", min, seconds);
    return 0;
}

allin myrtle:~$ gcc -Wall `pkg-config --cflags glib-2.0` pad.c  
`pkg-config --libs glib-2.0`
allin myrtle:~$ ./a.out 
test    = '08:05.1'
C native: '08:05.1'

Allin Cottrell



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]