Outline of a complete example of filling using the format

all

I want to change an element to a formatted string, then I use the format function. (the language I use is the circuit)

As stated in the document http://www.gnu.org/software/mit-scheme/documentation/mit-scheme-ref/Format.html , I can use ~ mincolA if I want to insert spaces on the right.

Therefore i use

(format "~4A " x) 

but I get this error:

format: ill-formed pattern string
  explanation: tag `~4' not allowed
  pattern string: "~4A "

I want to get the result as shown below:

if x is 0, then the result will be the space of space 0;

if x is 12, then the result is space 12.

I know I can use

(string-append (make-string (- 4 (string-length x)) #\ ) x)

to get the result that I want, but I really want to use the "format" function.

Thank.

+4
3

, MIT/GNU Scheme, format Racket -. - ~a :

(~a x #:min-width 4 #:align 'right #:left-pad-string " ") ; x can be a number or a string

:

(~a 0 #:min-width 4 #:align 'right #:left-pad-string " ")
=> "   0"

(~a "12" #:min-width 4 #:align 'right #:left-pad-string " ")
=> "  12"

, @uselpa -.

+3

format SRFI 48:

> (require srfi/48)
> (format "~4F" 0)
"   0"
> (format "~4F" 12)
"  12"

format , SRFI 48:

> (require (prefix-in srfi48: srfi/48))
> (srfi48:format "~4F" 0)

format - .

+3

format, SRFI-48. MIT Scheme, #! Racket ().

#!r6rs
(import (rnrs base)
        (srfi :48))

(format "~4F " "x") ; ==> "   x" 

SRFI-48 #! :

#!racket
(require srfi/48)

(format "~4F " "x") ; ==> "   x" 

F :

~ [w [, d]] F Fixed ~ w, dF displays a number with a width of w and d digits after the decimal point; ~ wF prints a string or number with a width of w.

Also, when evaluating (format "~h"), you get instructions for use, so you don’t need to visit the SRFI page for a basic syntax reminder.

+1
source

All Articles