On this page:
3.1 Numbers and Currency
format-number
format-currency
3.2 Dates and Times
format-date
format-time
format-datetime
8.12

3 Module locale/format🔗ℹ

 (require locale/format) package: racket-locale

This module provides functions to format numbers, currency, and date/time values. The formats are locale-specific and retrieved from Module locale/language-info.

3.1 Numbers and Currency🔗ℹ

procedure

(format-number value)  string?

  value : number?
Format a plain numeric value according to locale-specific conventions. In general this implies ensuring the correct decimal point and thousands separator characters as well as thousands grouping.

Examples:
> (require locale
           locale/format)
> (set-locale (make-locale-string "en"
                                  "US"
                                  #:code-page (normalize-code-page 'utf-8)))

#f

> (format-number 1234567.89)

"1234567.89"

procedure

(format-currency value [internationl])  string?

  value : number?
  internationl : boolean? = #f
Formatting a currency follows many of the conventions of format-number but also has to handle placement of currency symbols and sign symbols (see struct locale? for more details).

Examples:
> (require locale
           locale/format)
> (set-locale (make-locale-string "zh"
                                  "CN"
                                  #:code-page (normalize-code-page 'utf-8)))

#f

> (format-currency 1234.89)

format-currency: broke its own contract

  promised: string?

  produced: #<void>

  in: the range of

      (->* (number?) (boolean?) string?)

  contract from:

      <pkgs>/racket-locale/locale/format.rkt

  blaming: <pkgs>/racket-locale/locale/format.rkt

   (assuming the contract is correct)

  at: <pkgs>/racket-locale/locale/format.rkt:19:3

> (set-locale (make-locale-string "ko"
                                  "KR"
                                  #:code-page (normalize-code-page 'utf-8)))

#f

> (format-currency 1234.89)

format-currency: broke its own contract

  promised: string?

  produced: #<void>

  in: the range of

      (->* (number?) (boolean?) string?)

  contract from:

      <pkgs>/racket-locale/locale/format.rkt

  blaming: <pkgs>/racket-locale/locale/format.rkt

   (assuming the contract is correct)

  at: <pkgs>/racket-locale/locale/format.rkt:19:3

> (set-locale (make-locale-string "es"
                                  "ES"
                                  #:code-page (normalize-code-page 'utf-8)))

#f

> (format-currency 1234.89)

format-currency: broke its own contract

  promised: string?

  produced: #<void>

  in: the range of

      (->* (number?) (boolean?) string?)

  contract from:

      <pkgs>/racket-locale/locale/format.rkt

  blaming: <pkgs>/racket-locale/locale/format.rkt

   (assuming the contract is correct)

  at: <pkgs>/racket-locale/locale/format.rkt:19:3

The international argument denotes whether the formatting should follow international standared conventions, and whether the local currency symbol should be replaced by a standard three letter currency identifier.

Examples:
> (require locale
           locale/format)
> (set-locale (make-locale-string "zh"
                                  "CN"
                                  #:code-page (normalize-code-page 'utf-8)))

#f

> (format-currency 1234.89 #t)

format-currency: broke its own contract

  promised: string?

  produced: #<void>

  in: the range of

      (->* (number?) (boolean?) string?)

  contract from:

      <pkgs>/racket-locale/locale/format.rkt

  blaming: <pkgs>/racket-locale/locale/format.rkt

   (assuming the contract is correct)

  at: <pkgs>/racket-locale/locale/format.rkt:19:3

3.2 Dates and Times🔗ℹ

procedure

(format-date value)  string?

  value : (or/c date? number?)
Formatting a date value comprises the ordering of the date components, any separator characters and any localized string elements.

Examples:
> (require locale
           locale/format
           racket/date)
> (define now (current-date))
> (set-locale (make-locale-string "en"
                                  "US"
                                  #:code-page (normalize-code-page 'utf-8)))

#f

> (format-date now)

"2/7/24"

> (set-locale (make-locale-string "zh"
                                  "CN"
                                  #:code-page (normalize-code-page 'utf-8)))

#f

> (format-date now)

"2/7/24"

procedure

(format-time value [am-pm])  string?

  value : (or/c date? number?)
  am-pm : boolean? = #f
Formatting a time value comprises the ordering of the time components, any separator characters and any localized string elements.

Examples:
> (require locale
           locale/format
           racket/date)
> (define now (current-date))
> (set-locale (make-locale-string "en"
                                  "US"
                                  #:code-page (normalize-code-page 'utf-8)))

#f

> (format-time now)

"19:52:55"

> (set-locale (make-locale-string "zh"
                                  "CN"
                                  #:code-page (normalize-code-page 'utf-8)))

#f

> (format-time now)

"19:52:55"

procedure

(format-datetime value)  string?

  value : (or/c date? number?)
Formatting a date-and-time value comprises the ordering of the various components, any separator characters and any localized string elements.

Examples:
> (require locale
           locale/format
           racket/date)
> (define now (current-date))
> (set-locale (make-locale-string "en"
                                  "US"
                                  #:code-page (normalize-code-page 'utf-8)))

#f

> (format-datetime now)

"Wed March  7 19:52:55 2024"

> (set-locale (make-locale-string "zh"
                                  "CN"
                                  #:code-page (normalize-code-page 'utf-8)))

#f

> (format-datetime now)

"Wed March  7 19:52:55 2024"