• AVR Freaks

Hot!sprintf() does not align right

Author
ThoWa
New Member
  • Total Posts : 11
  • Reward points : 0
  • Joined: 2019/08/23 23:45:40
  • Location: 0
  • Status: offline
2019/08/31 09:51:59 (permalink)
0

sprintf() does not align right

Hi,
 
I'm having trouble with sprintf().
I want to right align the output and would expect leading spaces.
But the result is always left aligned.
 
    uint8_t Str_1[10] = {0};
    uint8_t Str_2[10] = {0};
    uint8_t Str_3[10] = {0};

    sprintf(Str_1, "%3.2f", 12.345); // -> "12.35"
    sprintf(Str_2, "%04.2f", 2.345); // -> "2.35"
    sprintf(Str_3, "% 3.1f", -123.4); // -> "-123.4"

 
I'm using MPLAP5.25 with XC8 and PIC18F42K47
 
#1

5 Replies Related Threads

    du00000001
    Just Some Member
    • Total Posts : 3064
    • Reward points : 0
    • Joined: 2016/05/03 13:52:42
    • Location: Germany
    • Status: offline
    Re: sprintf() does not align right 2019/09/03 09:48:29 (permalink)
    +1 (1)
    IIRC, the format descriptor is <total # of digits>.<trailing number of digits>
    When it comes to *print()ing, the format descriptor is considered a non-binding "wish": you'll get the number of digits required in front of the deci-point plus the number of trailing digits.
    For what you seen to see, "%8.2f" might work.

    PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
    #2
    LdB_ECM
    Senior Member
    • Total Posts : 148
    • Reward points : 0
    • Joined: 2019/04/16 22:01:25
    • Location: 0
    • Status: offline
    Re: sprintf() does not align right 2019/09/03 10:06:55 (permalink)
    -1 (1)
    Update sorry you want right justify
    Try putting the space in as \32 rather than a literal space but many wont allow anything but 0 but if it's going to work Du0 is on it and most default to space (The library function should be described in the manual you may have to RTFM). Other than that manually pad it on the write you know the string length you know how many characters you want.
    post edited by LdB_ECM - 2019/09/03 10:44:23
    #3
    ric
    Super Member
    • Total Posts : 23855
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: sprintf() does not align right 2019/09/03 16:59:15 (permalink)
    +2 (2)
    LdB_ECM
    ...
    Try putting the space in as \32 rather than a literal space but many wont allow anything but 0 but if it's going to work

    That's pointless.
    As already explained, the whole problem is that the number before the "." is too small in every case.
     

        sprintf(Str_1, "%8.2f", 12.345); // ->  "   12.35"
        sprintf(Str_2, "%08.2f", 2.345); // ->  "00002.35"
        sprintf(Str_3, "% 8.1f", -123.4); // -> "  -123.4"


    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #4
    1and0
    Access is Denied
    • Total Posts : 9756
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: sprintf() does not align right 2019/09/03 17:02:39 (permalink)
    +1 (1)
    The number before the "." is the Width field which specifies the minimum number of characters to output. Have a read of this:
     
    https://en.wikipedia.org/wiki/Printf_format_string
    #5
    ThoWa
    New Member
    • Total Posts : 11
    • Reward points : 0
    • Joined: 2019/08/23 23:45:40
    • Location: 0
    • Status: offline
    Re: sprintf() does not align right 2019/09/22 12:52:14 (permalink)
    0
    Thanks guys, that helped a lot !!!
    #6
    Jump to:
    © 2019 APG vNext Commercial Version 4.5