[racket] ~r behavior with respect to the sign

From: Dmitry Pavlov (dpavlov at ipa.nw.ru)
Date: Tue Dec 24 10:30:43 EST 2013

Hello,

It seems that ~r formatting function, when given a negative
number and a #:min-width argument, does not treat the "-"
character as part of the number bound by #:min-width,
and moreover, it detaches the "-" from the number:

 > (~r -34.567 #:precision '(= 2) #:min-width 7)
"-  34.57"

As an example of a more expected behavior, see the
programs in C and Fortran:


#include <stdio.h>

int main (void)
{
   printf("%7.2lf\n", -34.567);
}


       PROGRAM TESTREAL
       IMPLICIT REAL*8(x)
       x = -34.567
       write(*,100) '', x
  100  format (A,F7.2)
       END


both print " -34.57"


As another pro argument for the C/Fortran behavior: if one wanted
to detach "-" from the number, he would easily do

(string-append (if (negative? x) "-" "")
                (~r (abs x) #:precision '(= 2) #:min-width 7)

while doing this thing backwards, i.e. attaching "-" to the
beginning of a space-padded number, is a more complicated task.


Are there any plans to get this functionality into ~r?
(or maybe it is already there and I do not know)


Best regards,

Dmitry

Posted on the users mailing list.