Menu

#239 sprintf with variable precision

None
closed
nobody
None
5
2018-11-09
2018-11-08
No

Hello,
I'd like to use sprintf's functionality to take the precision as an argument. In bash, this looks like following (German locale with comma as decimal symbol)):
for i in {1..10}; do printf '%.*f \n' "$i" "1,234567890123"; done
The equivalent in gnuplot should be:
do for [i=1:10] {print sprintf("%.*f",i,1.234567890123)}
However, this takes i as the input for %f and therefore displays 1.0, 2.0 etc. Am I missing something, or is this an issue with the implementation?

Discussion

  • Eldrad Ulthran

    Eldrad Ulthran - 2018-11-08

    I should mention that I use Version 5.3 patchlevel 0 last modified 2018-11-02

     
  • Ethan Merritt

    Ethan Merritt - 2018-11-08

    Gnuplot's sprintf implementation does not cover the full range of the current glibc implementation. In particular it does not include format variants that were added for C99 or POSIX, and it does not include the dynamic field width options that you ask about.

    You could, however, have gnuplot perform that step for you when constructing the format:

     myformat(i) = sprintf("%%.%df", i)
     do for [i=1:10] { print sprintf( myformat(i), 1.2345678901) }
    
     
    • Eldrad Ulthran

      Eldrad Ulthran - 2018-11-09

      The nested sprintf calls work just like I wanted, thank you! Luckily I don't have too many of these so that the performance of plotting is not affected too much.

       
  • Ethan Merritt

    Ethan Merritt - 2018-11-09
    • status: open --> closed
    • Group: -->
     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.