Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

print integer with leading zeroes and sign

60 views
Skip to first unread message

Antony

unread,
Nov 22, 2010, 12:56:25 PM11/22/10
to
Given 7, I want to print +07

I tried
> (format t "~2,'0D" 7)
07
NIL
> (format t "~2@D" 7)
+7
NIL

Couldn't figure out how to combine the above.
(format t "~2,'0@D" 7) didn't work

Help please

-Antony

Peder O. Klingenberg

unread,
Nov 22, 2010, 2:23:17 PM11/22/10
to
Antony <spam+lisp...@gmail.com> writes:

> Couldn't figure out how to combine the above.
> (format t "~2,'0@D" 7) didn't work

I don't think it's possible with only one directive, since the padding
happens in front of the sign:

CL-USER> (format t "~3,'0@D" 7)
0+7

Ick.

I'd probably end up with something like

CL-USER> (format t "~:[+~;-~]~2,'0D" (sign 7) (abs 7))
+07

But it's not pretty. I'd love to see a variant with only one format
argument, if that's possible. (and without using ~/ - that's cheating
:) )

...Peder...
--
Sl�v uten dop.

Mirko

unread,
Nov 23, 2010, 10:09:36 AM11/23/10
to

You may need to write your own custom formatting function. See:

http://www.lispworks.com/documentation/HyperSpec/Body/22_ced.htm.

Mirko

Antony

unread,
Nov 23, 2010, 12:31:50 PM11/23/10
to

Rob Warnock

unread,
Nov 24, 2010, 11:14:26 PM11/24/10
to
Antony <spam+lisp...@gmail.com> wrote:
+---------------

| Mirko wrote:
| > You may need to write your own custom formatting function. See:
| > http://www.lispworks.com/documentation/HyperSpec/Body/22_ced.htm.
...

| I'll try the '~/foo/' stuff
+---------------

A reminder that the package for the name in "~/name/" defaults
to COMMON-LISP-USER, *not* the current value of *PACKAGE*.

Yes, I know it says that quite clearly, but it's easy to overlook
and can cause confusion later when you move from playing with stuff
in the default REPL to packaging up [pardon the pun] your code in
a separate module.


-Rob

-----
Rob Warnock <rp...@rpw3.org>
627 26th Avenue <URL:http://rpw3.org/>
San Mateo, CA 94403 (650)572-2607

0 new messages