Non-decimal radices/Input: Difference between revisions

m
m (→‎{{header|Wren}}: Minor tidy)
(3 intermediate revisions by 3 users not shown)
Line 295:
String '100' in base 19 is 361 in base 10
String '100' in base 20 is 400 in base 10</pre>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
Delphi has built in functions that can input numbers in decimal and hexadecimal. Here is simple subroutine that can input number in any radix from 2 to 36.
 
<syntaxhighlight lang="Delphi">
 
function InputByRadix(S: string; Radix: integer): integer;
{Coverts the input string of the specified radix to an integer}
{Accepts digits in the range 0..9 and A..Z and ignores anything else}
var I,B: integer;
begin
Result:=0;
S:=UpperCase(S);
for I:=1 to Length(S) do
begin
if S[I] in ['0'..'9'] then B:=byte(S[I])-$30
else if S[I] in ['A'..'Z'] then B:=byte(S[I])-$41;
Result:=Result * Radix + B;
end;
end;
 
procedure ShowRadixInput(Memo: TMemo);
var Base,I: integer;
begin
for Base:=2 to 20 do
begin
I:=InputByRadix('100',Base);
Memo.Lines.Add(Format('String "100" in base %2D is %3D in Base 10',[Base,I]));
end;
end;
 
</syntaxhighlight>
{{out}}
<pre>
String "100" in base 2 is 4 in Base 10
String "100" in base 3 is 9 in Base 10
String "100" in base 4 is 16 in Base 10
String "100" in base 5 is 25 in Base 10
String "100" in base 6 is 36 in Base 10
String "100" in base 7 is 49 in Base 10
String "100" in base 8 is 64 in Base 10
String "100" in base 9 is 81 in Base 10
String "100" in base 10 is 100 in Base 10
String "100" in base 11 is 121 in Base 10
String "100" in base 12 is 144 in Base 10
String "100" in base 13 is 169 in Base 10
String "100" in base 14 is 196 in Base 10
String "100" in base 15 is 225 in Base 10
String "100" in base 16 is 256 in Base 10
String "100" in base 17 is 289 in Base 10
String "100" in base 18 is 324 in Base 10
String "100" in base 19 is 361 in Base 10
String "100" in base 20 is 400 in Base 10
</pre>
 
 
=={{header|E}}==
Line 1,404 ⟶ 1,461:
-987654321
</pre>
 
=={{header|RPL}}==
Floating-point numbers can only be entered in decimal format:
3.14
Unsigned integers, which must begin with <code>#</code>, can be expressed in binary, octal, decimal or hexadecimal. A final lowercase letter defines the base.
#100111010b
#472o
#314d
#13Ah
 
=={{header|Ruby}}==
Line 1,522 ⟶ 1,588:
say bin_noprefix.bin; # => 345
say bin_withprefix.bin; # => 345</syntaxhighlight>
 
=={{header|SparForte}}==
As a structured script.
<syntaxhighlight lang="ada">#!/usr/local/bin/spar
pragma annotate( summary, "radices" )
@( description, "This task requires parsing of such a string (which may" )
@( description, "be assumed to contain nothing else) using the" )
@( description, "language's built-in facilities if possible. Parsing of" )
@( description, "decimal strings is required, parsing of other formats" )
@( description, "is optional but should be shown (i.e., if the language" )
@( description, "can parse in base-19 then that should be illustrated)." )
@( category, "tutorials" )
@( author, "Ken O. Burtch" )
@( see_also, "http://rosettacode.org/wiki/Non-decimal_radices/Input" );
pragma license( unrestricted );
 
pragma software_model( nonstandard );
pragma restriction( no_external_commands );
 
procedure radices is
begin
? numerics.value( "16#ABCF123#" );
? numerics.value( "8#7651#" );
? numerics.value( "2#1010011010#" );
? numerics.value( "16#F.FF#E+2" );
end radices;</syntaxhighlight>
 
=={{header|Standard ML}}==
Line 1,574 ⟶ 1,666:
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Conv, Fmt
 
var tests = [ ["0b1110", 2], ["112", 3], ["0o16", 8], ["14", 10], ["0xe", 16], ["e", 19] ]
for (test in tests) {
SystemFmt.print("%(Fmt.$6s in base $-2d = $s(6", test[0])) in base %(Fmt.d(-2, test[1])), = %(Conv.atoi(test[0], test[1]))")
} </syntaxhighlight>
 
9,485

edits