Increment a numerical string: Difference between revisions

(→‎tonumber: gojq, supports unbounded-precision integer arithmetic)
Tag: Manual revert
 
(20 intermediate revisions by 13 users not shown)
Line 8:
=={{header|11l}}==
{{trans|Python}}
 
<syntaxhighlight lang="11l">V next = String(Int(‘123’) + 1)</syntaxhighlight>
 
 
=={{header|8080 Assembly}}==
Line 70 ⟶ 68:
 
=={{header|8086 Assembly}}==
 
<syntaxhighlight lang="asm"> cpu 8086
bits 16
Line 392 ⟶ 389:
 
=={{header|APL}}==
 
<syntaxhighlight lang="apl">⍕1+⍎'12345'</syntaxhighlight>
{{Out}}
Line 548 ⟶ 544:
-- Catered-for exponent symbol in the input string. Set the output style to "scientific".
its setNumberStyle:(|⌘|'s NSNumberFormatterScientificStyle)
set symbol toits setExponentSymbol:(str's substringWithRange:(symbolRange))
its setExponentSymbol:(symbol)
else
-- Straight numerical text, with or without separators as per the input and locale.
Line 801 ⟶ 796:
.align 4
.Ls_magic_number_10: .word 0x66666667
 
</syntaxhighlight>
Line 866 ⟶ 860:
==={{header|Applesoft BASIC}}===
<syntaxhighlight lang="gwbasic">S$ = "12345":S$ = STR$ ( VAL (S$) + 1)</syntaxhighlight>
 
==={{header|BASIC256}}===
<syntaxhighlight lang="freebasic">cadena$ = "12345"
Line 896 ⟶ 891:
 
==={{header|ZX Spectrum Basic}}===
 
The ZX Spectrum needs line numbers and a let statement,
but the same technique can be used:
 
<syntaxhighlight lang="zxbasic">10 LET s$ = "12345"
20 LET s$ = STR$(VAL(s$) + 1)</syntaxhighlight>
Line 939 ⟶ 932:
 
=={{header|Bracmat}}==
Numbers are strings. Bracmat supports rational numbers, including integers, using arbitrary-precision arithmetic. Pure imaginary numbers are formed using a factor <code>i</code> (or <code>-i</code>). There is no support for floating point arithmetics. (Historically, because the ARM 2 processor in the Archimedes computer didn't sport an FPU.)
<syntaxhighlight lang="bracmat">(n=35664871829866234762187538073934873121878/6172839450617283945)
&!n+1:?n
Line 946 ⟶ 939:
35664871829866234762193710913385490405823/6172839450617283945
</syntaxhighlight>
Output
<pre></pre>
 
Bracmat also supports floating point numbers, but only in a sandboxed way. To increment 0.234E0 by 1:
 
<syntaxhighlight lang="bracmat">(new$(UFP,(=(s.val).!val+1)).go)$"0.234e0"</syntaxhighlight>
Output
<pre>1.2340000000000000E+00</pre>
 
<code>UFP</code> is a new (2023) object type that specializes in compiling and executing floating point operations. Bracmat code must be passed when a UFP object is created. This code has a more restricted grammar than Bracmat code in general. The passed code must be the definition of a function that can take one or more scalars or arrays as argument. In the example, this function is <code>(=(s.val).!val+1)</code>. The expression <code>(s.val)</code> says that the function takes a scalar and binds it to the local variable <code>val</code>. In the example, the function is defined, compiled, executed and destroyed in one go. This is not necessary; it is perfectly possible to use the compiled UFP object multiple times before destroying it.
 
=={{header|Brat}}==
Line 952 ⟶ 955:
 
=={{header|Burlesque}}==
 
<syntaxhighlight lang="burlesque">
ri?ish
Line 1,249 ⟶ 1,251:
assert(s == "12350");
}</syntaxhighlight>
 
=={{header|dc}}==
<syntaxhighlight lang="dc">? 1 + p</syntaxhighlight>
The program expects a string on ''stdin'':
<syntaxhighlight lang="sh">echo '12345678899' | dc inc.dc</syntaxhighlight>
{{out}}
<pre>12345678900</pre>
 
=={{header|Delphi}}==
Line 1,267 ⟶ 1,276:
{{Out}}
<pre>"12345" + 1 = 123456</pre>
 
=={{header|dt}}==
<syntaxhighlight lang="dt">"1234567889" to-int 1 + to-string</syntaxhighlight>
 
=={{header|Dyalect}}==
Line 1,397 ⟶ 1,409:
 
=={{header|Emacs Lisp}}==
 
<syntaxhighlight lang="lisp">(1+ (string-to-number "12345"))</syntaxhighlight>
 
=={{header|ErlangEMal}}==
<syntaxhighlight lang="emal">
fun incrementGeneric = text by generic T, text numerical
return text!(:T!numerical + 1)
end
fun increment = text by text numerical
return incrementGeneric(when(numerical.contains("."), real, int), numerical)
end
writeLine(incrementGeneric(real, "123.32"))
writeLine(incrementGeneric(int, "123"))
writeLine(increment("123.32"))
writeLine(increment("123"))
</syntaxhighlight>
{{out}}
<pre>
124.32
124
124.32
124
</pre>
 
=={{header|Erlang}}==
<syntaxhighlight lang="erlang">integer_to_list(list_to_integer("1336")+1).</syntaxhighlight>
 
Line 1,427 ⟶ 1,458:
 
=={{header|F_Sharp|F#}}==
<syntaxhighlight lang="fsharp">let next = string( int( "1234" ) + 1 )</syntaxhighlight>
// Increment a numerical string. Nigel Galloway: April 4th., 2023
let inc=int>>(+)1>>string
printfn "%s" (inc("1234"))
</syntaxhighlight>
{{out}}
<pre>
1235
</pre>
 
=={{header|Factor}}==
Line 1,433 ⟶ 1,472:
 
=={{header|Fantom}}==
 
Within 'fansh':
 
Line 1,519 ⟶ 1,557:
<syntaxhighlight lang="frink">a = input["Enter number: "]
toString[eval[a] + 1]</syntaxhighlight>
 
=={{header|FTCBASIC}}==
<syntaxhighlight lang="basic">define value = 0, text$ = "12345"
 
strint value,text$
+1 value
intstr text$,value
 
print text$
pause
end</syntaxhighlight>
 
=={{header|FutureBasic}}==
Line 1,549 ⟶ 1,598:
=={{header|Fōrmulæ}}==
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Increment_a_numerical_string}}
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text. Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation &mdash;i.e. XML, JSON&mdash; they are intended for storage and transfer purposes more than visualization and edition.
 
'''Solution'''
 
[[File:Fōrmulæ - Increment a numerical string 01.png]]
Programs in Fōrmulæ are created/edited online in its [https://formulae.org website], However they run on execution servers. By default remote servers are used, but they are limited in memory and processing power, since they are intended for demonstration and casual use. A local server can be downloaded and installed, it has no limitations (it runs in your own computer). Because of that, example programs can be fully visualized and edited, but some of them will not run if they require a moderate or heavy computation/memory resources, and no local server is being used.
 
[[File:Fōrmulæ - Increment a numerical string 02.png]]
In '''[https://formulae.org/?example=Increment_a_numerical_string this]''' page you can see the program(s) related to this task and their results.
 
=={{header|Gambas}}==
Line 1,949 ⟶ 2,000:
<pre>42 1492.3 -0.5 137
42 pine martens in 1492.3 -0.5 mushrooms ≠ 137</pre>
 
=={{header|Joy}}==
<syntaxhighlight lang="jq">DEFINE incstr == 10 strtol succ 'd 1 1 format.
 
"1234567889" incstr.</syntaxhighlight>
{{out}}
<pre>"1234567890"</pre>
 
=={{header|jq}}==
Line 2,054 ⟶ 2,112:
</pre>
 
=={{header|LambdatalkLabVIEW}}==
{{VI snippet}}<br/>
[[File:LabVIEW_Increment_a_numerical_string.png]]
 
=={{header|Lambdatalk}}==
<syntaxhighlight lang="scheme">
In lambdatalk every expression is a word or an S-expression, so:
Line 2,069 ⟶ 2,130:
</syntaxhighlight>
 
=={{header|LabVIEWLang}}==
<syntaxhighlight lang="lang">
{{VI snippet}}<br/>
$next $= text(+|int({{{123}}}))
[[File:LabVIEW_Increment_a_numerical_string.png]]
</syntaxhighlight>
 
=={{header|Lasso}}==
Line 2,134 ⟶ 2,196:
 
=={{header|LOLCODE}}==
 
LOLCODE is weakly typed, so the arithmetic operators work "as expected" on strings.
 
Line 2,232 ⟶ 2,293:
numStr = num2str(str2double(numStr) + 1);
end</syntaxhighlight>
 
=={{header|Maxima}}==
<syntaxhighlight lang="maxima">
inc_string(str):=if stringp(str) and numberp(eval_string(str)) then string(eval_string(str)+1)$
"12345"$
inc_string(%);
</syntaxhighlight>
{{out}}
<pre>
"12346"
</pre>
 
=={{header|MAXScript}}==
Line 2,477 ⟶ 2,549:
 
=={{header|Octave}}==
 
We convert the string to a number, increment it, and convert it back to a string.
 
Line 2,781 ⟶ 2,852:
 
=={{header|plainTeX}}==
 
<syntaxhighlight lang="tex">\newcount\acounter
\def\stringinc#1{\acounter=#1\relax%
Line 2,871 ⟶ 2,941:
 
=={{header|Quackery}}==
 
As a dialogue in the Quackery shell.
 
Line 2,907 ⟶ 2,976:
=={{header|Raku}}==
(formerly Perl 6)
{{works with|Rakudo|#22 "Thousand Oaks"}}
 
<syntaxhighlight lang="raku" line>say my $s = "12345";
say ++$s;
$s++;</syntaxhighlight>
 
# or Unicode. How about Sinhala?
 
say "෧෨෩෪෫ ", +"෧෨෩෪෫";
say "෧෨෩෪෫".succ, ' ', +"෧෨෩෪෫".succ;</syntaxhighlight>
{{out}}
<pre>12345
12346
෧෨෩෪෫ 12345
෧෨෩෪෬ 12346</pre>
 
=={{header|Rascal}}==
Line 2,953 ⟶ 3,031:
 
=={{header|Retro}}==
 
<syntaxhighlight lang="retro">'123 s:to-number n:inc n:to-string</syntaxhighlight>
 
=={{header|REXX}}==
 
REXX, like many other scripting languages, uses typeless variables.
<br>Typeless variables are stored as variable length character strings and can be treated as
Line 3,049 ⟶ 3,125:
=={{header|RPL}}==
Conversion to/from a real number is the most convenient way to perform the task.
{{in}}
"1234" STR→ 1 + →STR
<pre>
"99" STR→ 1 + →STR
"1234" STR→ 1 + →STR
2: "1235"
"99.9" STR→ 1 + →STR
1: "100"
</pre>
{{out}}
<pre>
2: "1235"
1: "100.9"
</pre>
 
=={{header|Ruby}}==
If a string represents a number, the succ method will increment the number:
Line 3,157 ⟶ 3,240:
124
</pre>
 
=={{header|SparForte}}==
As a structured script.
<syntaxhighlight lang="ada">#!/usr/local/bin/spar
pragma annotate( summary, "incstr" )
@( description, "Increment an integer number in a string" )
@( category, "tutorials" )
@( author, "Ken O. Burtch" )
@( see_also, "http://rosettacode.org/wiki/Increment_a_numerical_string" );
pragma license( unrestricted );
 
pragma software_model( nonstandard );
pragma restriction( no_external_commands );
 
procedure incstr is
s : string := "12345";
begin
s := strings.trim( strings.image( integer( numerics.value( s ) + 1 ) ), trim_end.both ) ;
? s;
end incstr;</syntaxhighlight>
 
=={{header|Sparkling}}==
Line 3,249 ⟶ 3,352:
 
=={{header|TXR}}==
 
Two implementations of what the task says: incrementing a numerical string. (Not: converting a string to a number, then incrementing the number, then converting back to string.)
 
Line 3,350 ⟶ 3,452:
 
=={{header|Ursala}}==
 
<syntaxhighlight lang="ursala">#import nat
 
Line 3,521 ⟶ 3,622:
 
=={{header|Wren}}==
<syntaxhighlight lang="ecmascriptwren">var ns = "41"
var n = Num.fromString(ns) + 1
var ns2 = "%(n)"
305

edits