# Formatting negative numbers using \`~r\`

**URL:** <https://racket.discourse.group/t/formatting-negative-numbers-using-r/2354>\
**Category:** General\
**Created:** [September 30, 2023, 11:18pm UTC](https://racket.discourse.group/t/formatting-negative-numbers-using-r/2354 "2023-09-30T23:18:33Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![alexh](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/alexh/32/315_2.png) [@alexh](https://racket.discourse.group/u/alexh)\
**Post date:** [September 30, 2023, 11:18pm UTC](https://racket.discourse.group/t/formatting-negative-numbers-using-r/2354/1 "2023-09-30T23:18:33Z")

</div>

I was trying to format numbers using `~r` to align them in a table, and noticed an unexpected, for me at least, result for negative numbers: the minus sign is put in front of the padding:

```scheme
> (~r (- pi) #:precision 2 #:min-width 10)
"- 3.14"

```

I suppose this makes sense if the pad-string is "0", but does not make sense for any other pad-string:

```scheme
> (~r (- pi) #:precision 2 #:min-width 10 #:pad-string "0")
"-0000003.14"
> (~r (- pi) #:precision 2 #:min-width 10 #:pad-string "#")
"-######3.14"

```

I can always combine `~a` and `~r` to get the desired result, but I am wondering if this is a defect/omission, or is it a convention I am not aware of?

```scheme
> (~a (~r (- pi) #:precision 2) #:min-width 10 #:align 'right)
" -3.14"

```

Thanks,  
Alex.

---

<div class="post-metadata">

**Author:** ![shawnw](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/shawnw/32/1031_2.png) [@shawnw](https://racket.discourse.group/u/shawnw)\
**Post date:** [October 2, 2023, 9:30pm UTC](https://racket.discourse.group/t/formatting-negative-numbers-using-r/2354/2 "2023-10-02T21:30:05Z")

</div>

From [the documentation for `~r`'s `#:pad-string` option](https://docs.racket-lang.org/reference/strings.html#%28def._%28%28lib._racket%2Fformat..rkt%29._~7er%29%29):

> The padding is placed between the sign and the normal digits of x.

And the default padding is a space, so this behavior, while not what I'd expect to see, is what's documented. Another oddity is that `#:min-width` doesn't count the sign as part of the length; your strings are 11 characters long, not 10.

I ported SLIB's implementation of the [Common Lisp `format`](http://www.lispworks.com/documentation/HyperSpec/Body/22_c.htm) to Racket as the [`slib-format`](https://docs.racket-lang.org/slib-format/index.html) package and use that for most text formatting, as it has a ton of options beyond the formatting functions that come with Racket and tends to Just Work:

```scheme
> (require slib/format)
> (format #f "~10,2F" (- pi))
" -3.14"
> (format #f "~10,2,,,'#F" (- pi))
"#####-3.14"

```
