Next: , Previous: , Up: formations   [Index]

52.6 Escape sequences for flonums

Format Escape Sequence: ~f

Real fixed–point float. Parameters: width, decimals, scale, overflowchar, padchar.

Output a number or number string in fixed-point format, ie. with a decimal point.

(format #t "~f" 5)              -| 5.0
(format #t "~f" 1e-1)           -| 0.1
(format #t "~f" "123")          -| 123.0
(format #t "~f" "#d123")        -| 123.0
(format #t "~f" "#d-1e-1")      -| -0.1

~@f prints a + sign on positive numbers (including zero).

(format #t "~@f" 0) -| +0.0

If the output is less than width characters (default is no limit and no padding) it’s padded on the left with padchar (space by default). If the output equals or exceeds width then there’s no padding and the decimals after the comma are truncated if this makes the number fit the width.

(format "~6f" -1.5)                     ⇒ "  -1.5"
(format "~6,,,,'*f" 23)                 ⇒ "**23.0"
(format "~6f" 1234567.0)                ⇒ "1234567.0"
(format "~10,,,,'.f" 123.456789123)     ⇒ "123.456789"
(format "~5,,,,'.f" 1e9)                ⇒ "1000000000.0"
(format "~5,,,,'.f" 1000000000.123456)  ⇒ "1000000000.123456"

decimals is how many digits to print after the decimal point, with the value rounded or padded with zeros as necessary. (The default is to output as many decimals as required.)

(format #t "~1,2f" 3.125) -| 3.13
(format #t "~1,2f" 1.5)   -| 1.50

Decimals are rounded only when requested. If not requested they are not rounded nor truncated when the output exceeds the requested width.

(format #t "~1f"   0.123) -| "0.123")
(format #t "~1,2f" 0.123) -| ".12")

scale is a power of 10 applied to the value, moving the decimal point that many places. A positive scale increases the value shown, a negative decreases it.

(format #t "~,,2f" 1234)  -| 123400.0
(format #t "~,,-2f" 1234) -| 12.34

If overflowchar and width are both given and if the output would exceed width, then that many overflowchars are printed instead of the value.

(format #t "~5,,,'xf" 12345) -| 12345
(format #t "~4,,,'xf" 12345) -| xxxx
Format Escape Sequence: ~e

Real exponential float. Parameters: width, decimals, expdigits, intdigits, overflowchar, padchar, expchar.

Output a number or number string in exponential notation.

(format #t "~e" 5000.25) -| 5.00025E+3
(format #t "~e" "123.4") -| 1.234E+2
(format #t "~e" "1e4")   -| 1.0E+4

~@e prints a + sign on positive numbers (including zero). (This is for the mantissa, a + or - sign is always shown on the exponent.)

(format #t "~@e" 5000.0) -| +5.0E+3

If the output is less than width characters it’s padded on the left with padchar (space by default). The default for width is to output with no padding.

(format #f "~10e" 1234.0)       ⇒ "  1.234E+3"
(format #f "~10,,,,,'*e" 0.5)   ⇒ "****5.0E-1"

If the output is more than width characters: decimals in the exponential representation are rounded to make the output fit the width; if rounding does not make the output fit, the full output without rounding is printed. When decimals is specified: no rounding is done to remove those decimals, even if the output exceeds the width.

;; here rounding succeeds in making the output fit
(format #f "~5e" 123456)        ⇒ "1.E+5"

;; here rounding fails in making the output fit
(format #f "~2e" 123456)        ⇒ "1.23456E+5"

;; here DECIMALS causes the output to overflow
(format "~6,3e" 123.3456)       ⇒ "1.233E+2"

decimals is the number of digits shown in the mantissa after the decimal point. The value is rounded or trailing zeros are added as necessary. The default decimals is to show as much as needed by the value.

(format #f "~,3e" 11111.0) ⇒ "1.111E+4"
(format #f "~,8e" 123.0)   ⇒ "1.23000000E+2"

expdigits is the minimum number of digits shown for the exponent, with leading zeros added if necessary. The default for expdigits is to show only as many digits as required. At least 1 digit is always shown.

(format #f "~,,1e" 1.0e99) ⇒ "1.0E+99"
(format #f "~,,6e" 1.0e99) ⇒ "1.0E+000099"

intdigits (default 1) is the number of digits to show before the decimal point in the mantissa. intdigits can be zero, in which case the integer part is a single 0, or it can be negative, in which case leading zeros are shown after the decimal point.

(format #t "~,,,3e" 12345.0)  -| 123.45E+2
(format #t "~,,,0e" 12345.0)  -| 0.12345E+5
(format #t "~,,,-3e" 12345.0) -| 0.00012345E+8

If overflowchar is given then width is a hard limit. If the output would exceed width then instead that many overflowchars are printed.

(format #f "~6,,,,'xe" 100.0) ⇒ "1.0E+2"
(format #f "~3,,,,'xe" 100.0) ⇒ "xxx"

expchar is the exponent marker character (default E).

(format #t "~,,,,,,'ee" 100.0) -| 1.0e+2

Next: , Previous: , Up: formations   [Index]