avr-gcc-list
[Top][All Lists]
Advanced

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

Re: [avr-gcc-list] sprintf - formated output as XXX.XXX.XXX


From: Richard Urwin
Subject: Re: [avr-gcc-list] sprintf - formated output as XXX.XXX.XXX
Date: Thu, 6 Jan 2005 19:07:19 +0000
User-agent: KMail/1.5.3

On Thursday 06 Jan 2005 5:39 pm, Dave Hansen wrote:
> From: honschu <address@hidden>
> [...]
>
> >sprintf( lcd_str1, "HSync %lu", value );
> >lcd_puts( lcd_str1 );
> >
> >the LCD prints out something like: "HSync 80000000"
> >but because of the big values, I would love to get a more readable
> > format output as "HSync 80.000.000".
> >
> >Can anyone please help me?
>
> I don't believe there is a standard way to do this.  The following is
> untested and not an extremely clever method, but should come close to
> working:
>
>    char const start_fmt[] = "%3lu";
>    char const cont_fmt[] = ",%03lu";
>    char cont = 0;
>    unsigned long temp;
>    unsigned long div;
>
>    for (div = 1000000000; div > 0; div/=1000)
>    {
>       temp = value/div;
>       if (cont)
>       {
>          printf(cont_fmt, temp);
>       }
>       else if (temp)

To get 0 output correctly:
 else if (temp != 0 || div == 1)

>       {
>          printf(start_fmt, temp);
>          cont = 1;
>       }
>       value -= temp*div;
>    }

The few times I have been tempted to use sprintf in an embedded 
application I have been appalled by its memory footprint, although I 
haven't tried it for the AVR. 

If you want to use sprintf, then you could always copy the source code 
and add the feature yourself.

-- 
Richard Urwin


reply via email to

[Prev in Thread] Current Thread [Next in Thread]