(number->string z) ¶(number->string z radix) ¶It is an error if radix is not one of 2, 8, 10, or 16.
The procedure number->string takes a number and a radix and returns
as a string an external representation of the given number in the given
radix such that
(let ((number number) (radix radix)) (eqv? number (string->number (number->string number radix) radix)))
is true. It is an error if no possible result makes this expression true. If omitted, radix defaults to 10.
If z is inexact, the radix is 10, and the above expression can be satisfied by a result that contains a decimal point, then the result contains a decimal point and is expressed using the minimum number of digits (exclusive of exponent and trailing zeroes) needed to make the above expression true34 35; otherwise the format of the result is unspecified.
The result returned by number->string never contains an explicit
radix prefix.
Note: The error case can occur only when z is not a complex number or is a complex number with a non-rational real or imaginary part.
Rationale: If z is an inexact number and the radix is 10, then the above expression is normally satisfied by a result containing a decimal point. The unspecified case allows for infinities, NaNs, and unusual representations.
(string->number string) ¶(string->number string radix) ¶Returns a number of the maximally precise representation expressed by the given string. It is an error if radix is not 2, 8, 10, or 16.
If supplied, radix is a default radix that will be overridden if
an explicit radix prefix is present in string (e.g.,
‘#o177’). If radix is not supplied, then the default
radix is 10. If string is not a syntactically valid notation for
a number, or would result in a number that the implementation cannot
represent, then string->number returns #f. An error is
never signaled due to the content of string.
(string->number "100") ⇒ 100 (string->number "100" 16) ⇒ 256 (string->number "1e2") ⇒ 100.0
Note: The domain of
string->numbermay be restricted by implementations in the following ways. If all numbers supported by an implementation are real, thenstring->numberis permitted to return#fwhenever string uses the polar or rectangular notations for complex numbers. If all numbers are integers, thenstring->numbermay return#fwhenever the fractional notation is used. If all numbers are exact, thenstring->numbermay return#fwhenever an exponent marker or explicit exactness aprefix is used. If all inexact numbers are integers, thenstring->numbermay return#fwhenever a decimal point is used.The rules used by a particular implementation for
string->numbermust also be applied toreadand to the routine that reads programs, in order to maintain consistency between internal numeric processing, I/O (input/output), and the processing of programs. As a consequence, the R5RS permission to return#fwhen string has an explicit radix prefix has been withdrawn.
Robert G. Burger and R. Kent Dybvig. Printing floating-point numbers quickly and accurately. In Proceedings of the ACM SIGPLAN ’96 Conference on Programming Language Design and Implementation, pages 108–116.
William Clinger. How to read floating point numbers accurately. In Proceedings of the ACM SIGPLAN ’90 Conference on Programming Language Design and Implementation, pages 92–101. Proceedings published as SIGPLAN Notices 25(6), June 1990.