1 USING: help.markup help.syntax math math.parser.private prettyprint
2 namespaces make strings ;
5 ARTICLE: "number-strings" "Converting between numbers and strings"
6 "These words only convert between real numbers and strings. Complex numbers are constructed by the parser (" { $link "parser" } ") and printed by the prettyprinter (" { $link "prettyprint" } ")."
8 "Integers can be converted to and from arbitrary bases. Floating point numbers can only be converted to and from base 10 and 16."
10 "Converting numbers to strings:"
18 "Converting strings to numbers:"
26 "You can also input literal numbers in a different base (" { $link "syntax-integers" } ")."
27 { $see-also "prettyprint-numbers" } ;
29 ABOUT: "number-strings"
32 { $values { "seq" "a sequence of integers" } { "radix" "an integer between 2 and 36" } { "n/f" { $maybe integer } } }
33 { $description "Converts a sequence of digits (with most significant digit first) into an integer." }
34 { $notes "This is one of the factors of " { $link string>number } "." } ;
37 { $values { "n" "an integer between 0 and 35" } { "ch" "a character" } }
38 { $description "Outputs a character representation of a digit." }
39 { $notes "This is one of the factors of " { $link number>string } "." } ;
42 { $values { "ch" "a character" } { "n" integer } }
43 { $description "Converts a character representation of a digit to an integer." }
44 { $notes "This is one of the factors of " { $link string>number } "." } ;
47 { $values { "str" string } { "radix" "an integer between 2 and 36" } { "n/f" "a real number or " { $link f } } }
48 { $description "Creates a real number from a string representation with the given radix. The radix is ignored for floating point literals; they are always taken to be in base 10."
50 "Outputs " { $link f } " if the string does not represent a number." } ;
52 { >base base> } related-words
55 { $values { "str" string } { "n/f" "a real number or " { $link f } } }
56 { $description "Creates a real number from a string representation of a number in base 10."
58 "Outputs " { $link f } " if the string does not represent a number." } ;
60 { string>number number>string } related-words
63 { $values { "str" string } { "n/f" "a real number or " { $link f } } }
64 { $description "Creates a real number from a string representation of a number in base 2."
66 "Outputs " { $link f } " if the string does not represent a number." } ;
68 { >bin POSTPONE: BIN: bin> .b } related-words
71 { $values { "str" string } { "n/f" "a real number or " { $link f } } }
72 { $description "Creates a real number from a string representation of a number in base 8."
74 "Outputs " { $link f } " if the string does not represent a number." } ;
76 { >oct POSTPONE: OCT: oct> .o } related-words
79 { $values { "str" string } { "n/f" "a real number or " { $link f } } }
80 { $description "Creates a real number from a string representation of a number in base 16."
82 "Outputs " { $link f } " if the string does not represent a number." } ;
84 { >hex POSTPONE: HEX: hex> .h } related-words
87 { $values { "n" real } { "radix" "an integer between 2 and 36" } { "str" string } }
88 { $description "Converts a real number into a string representation using the given radix. If the number is a float, the radix is ignored and the output is always in base 10." } ;
91 { $values { "n" real } { "str" string } }
92 { $description "Outputs a string representation of a number using base 2." } ;
95 { $values { "n" real } { "str" string } }
96 { $description "Outputs a string representation of a number using base 8." } ;
99 { $values { "n" real } { "str" string } }
100 { $description "Outputs a string representation of a number using base 16." }
103 "USING: math.parser prettyprint ;"
108 "USING: math.parser prettyprint ;"
114 HELP: string>float ( str -- n/f )
115 { $values { "str" string } { "n/f" "a real number or " { $link f } } }
116 { $description "Primitive for creating a float from a string representation." }
117 { $notes "The " { $link string>number } " word is more general."
119 "Outputs " { $link f } " if the string does not represent a float." } ;
122 { $values { "n" real } { "str" string } }
123 { $description "Primitive for getting a string representation of a float." }
124 { $notes "The " { $link number>string } " word is more general." } ;
127 { $values { "n" real } { "str" string } }
128 { $description "Converts a real number to a string." }
129 { $notes "Printing complex numbers requires the more general prettyprinter facility (see " { $link "prettyprint" } ")." } ;
132 { $values { "n" real } }
133 { $description "Appends the string representation of a real number to the end of the sequence being constructed by " { $link make } "." } ;