"Scale by 10^d and round" scheme works mostly, and I use it often when the rare case doesn't matter.
I vaguely remember there're fail cases due to double rounding (scaling by 10^d requires more bits than
the original number, causing the spilled bits to be rounded by floating-point calculation, and in rare cases,
that rounding crosses over the midpoint of our rounding).   I couldn't find the actual case now, though.


On Fri, Oct 13, 2017 at 7:52 AM, Donald Allen <xxxxxx@gmail.com> wrote:
On 13 October 2017 at 13:51, Marc Feeley <xxxxxx@iro.umontreal.ca> wrote:
> I’ve found this code works well for me…
>
> (define (num->string num w d) ; w = total width, d = decimals
>  (let ((n (round (* (abs (inexact->exact num)) (expt 10 d)))))
>    (let ((i (quotient n (expt 10 d)))
>          (f (modulo n (expt 10 d))))
>      (let ((si (string-append
>                  (if (< num 0) "-" "")
>                  (number->string i 10)))
>            (sf (if (> d 0)
>                    (let ((sf (number->string (+ f (expt 10 d)) 10)))
>                      (string-set! sf 0 #\.)
>                      sf)
>                    "")))
>        (let ((lsi (string-length si))
>              (lsf (string-length sf)))
>          (let ((blanks (- w (+ lsi lsf))))
>            (string-append (make-string (max blanks 0) #\space) si sf)))))))
>
> Perhaps that’s better for your purpose than srfi-48.

Thank you. I will give it a try.

/Don