Numeric separator syntax: Difference between revisions

m
m (→‎{{header|Python}}: only supported in python 3.6+)
m (→‎{{header|Wren}}: Minor tidy)
 
(21 intermediate revisions by 16 users not shown)
Line 2:
 
Several programming languages allow separators in numerals in order to group digits together.
 
 
;Task:
Show the numeric separator syntax and describe its specification. E.g., what separators are eligible? Can there be multiple consecutive separators? What position can a separator be in? Etc.
 
 
;E.G.:
:*   What separators are eligible?
:*   Can there be multiple consecutive separators?
:*   What position can a separator be in?
:*   Etc.
<br><br>
 
=={{header|11l}}==
The apostrophe, <code>'</code>, is used as a digit separator.
3-digit groups [from the right] should be used in decimal numeric literals.
<syntaxhighlight lang="11l">print(100'000) // correct numeric literal
print(1'00000) // wrong numeric literal</syntaxhighlight>
 
=={{Header|Ada}}==
The Ada language uses the underscore '_' as a digit separator. The underscore separator must be between digits.
<syntaxhighlight lang="ada">with Ada.Text_IO; use Ada.Text_IO;
with Ada.Float_Text_IO; use Ada.Float_Text_IO;
 
procedure Main is
type u64 is mod 2**64;
pi : constant Float :=
3.14159_26535_89793_23846_26433_83279_50288_41971_69399_37511;
Trillion : u64 := 1_000_000_000_000;
begin
Put ("pi : ");
Put (Item => pi, Exp => 0, Aft => 7);
New_Line;
Put_Line ("Trillion : " & Trillion'Image);
end Main;</syntaxhighlight>
{{out}}
<pre>
pi : 3.141593
Trillion : 1000000000000
</pre>
 
=={{header|ALGOL 68}}==
In Algol 68, spaces are not significant in identifiers or numeric literals. This allows spaces to be used as numeric separators.
<br>Single or multiple spaces can be used as desired, it is not necessary to group the digits into blocks of three.
<langsyntaxhighlight lang="algol68">BEGIN
INT a = 1 234 567;
REAL b = 3 . 1 4159 26 5 359;
print( ( whole( a, 0 ), " ", fixed( b, - 14, 11 ), newline ) )
END
</syntaxhighlight>
</lang>
{{out}}
<pre>
1234567 3.14159265359
</pre>
 
=={{header|Arturo}}==
 
Arturo does not have numeric separator syntax.
 
Numbers can be either normal integers (without any separators whatsoever) or floating-point numbers.
 
<syntaxhighlight lang="rebol">a: 1234567
b: 3.14
 
print a
print b</syntaxhighlight>
 
{{out}}
 
<pre>1234567
3.14</pre>
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f NUMERIC_SEPARATOR_SYNTAX.AWK
# converted from ALGOL 68
Line 33 ⟶ 85:
exit(0)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 40 ⟶ 92:
 
=={{header|C}}==
locale.h provides Localization functions and is part of the C Standard Library. Separating digits in code text iswas not possible until C23.
<syntaxhighlight lang="c">
<lang C>
#include <locale.h>
#include <stdio.h>
Line 55 ⟶ 107:
return 0;
}
</syntaxhighlight>
</lang>
Output :
<pre>
Line 61 ⟶ 113:
Locale : C, One Trillion : 1,000,000,000,000
</pre>
 
=={{header|C++}}==
C++14 introduced the apostrophe (') as a numeric separator. The specification is [http://open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3781.pdf n3781]. The code below will thus run only on a C++14 or later compiler :
<syntaxhighlight lang="cpp">
//Aamrun, 4th October 2021
 
#include <iostream>
using namespace std;
 
int main()
{
long long int a = 30'00'000;
 
std::cout <<"And with the ' in C++ 14 : "<< a << endl;
 
return 0;
}
</syntaxhighlight>
{{out}}
<pre>
And with the ' in C++ 14 : 3000000
</pre>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|}}
Delphi doesn't support any alternate number separators for compiler source code input. However, Delphi does support virtually any character for thousand and decimal separators. The following code shows how to override the Windows' thousands and decimal separator conventions so it support only the US standard. This is useful when reading and writing files. If you don't do this, files written in country may be unreadable in another country. This same technique can be used to set any decimal and thousands separator you want.
 
<syntaxhighlight lang="Delphi">
procedure SetInternational(Flag: boolean);
{Enable/Disable International Support }
var DefaultLCID: Integer;
begin
InternationalFlag:=Flag;
DefaultLCID := GetThreadLocale;
if Flag then
begin
{This gets a "platform" warning}
{$WARNINGS OFF}
IndSystemData.DecimalSeparator := GetLocaleChar(DefaultLCID, LOCALE_SDECIMAL, '.');
IndSystemData.ThousandSeparator := GetLocaleChar(DefaultLCID, LOCALE_STHOUSAND, ',');
{$WARNINGS ON}
end
else
begin
IndSystemData.DecimalSeparator:='.';
{No thousands separator so we can parse comma separated data}
IndSystemData.ThousandSeparator:=#0;
end;
end;
 
</syntaxhighlight>
{{out}}
<pre>
 
</pre>
 
 
=={{header|Factor}}==
Factor allows the comma <code>,</code> as a separator character in number literals.
<langsyntaxhighlight lang="factor">USE: prettyprint
 
12,345 . ! 12345
Line 89 ⟶ 198:
 
! and complex numbers
C{ 5.225,312 2.0 } . ! C{ 5.225312 2.0 }</langsyntaxhighlight>
 
If one desires to define a syntax for different separator rules, that is possible:
<langsyntaxhighlight lang="factor">USING: lexer math.parser prettyprint sequences sets ;
 
<< SYNTAX: PN: scan-token "_" without string>number suffix! ; >>
Line 99 ⟶ 208:
PN: _1_2_3_ . ! 123
PN: 1__234___567 . ! 1234567
PN: 0b0___10.100001p3 . ! 20.125</langsyntaxhighlight>
 
Since Factor's parser is exposed, one could even make changes to the number parser, obviating the need for parsing words.
<langsyntaxhighlight lang="factor">USING: eval prettyprint ;
 
<<
Line 119 ⟶ 228:
>>
 
3_333_333 . ! 3333333</langsyntaxhighlight>
 
<!-- == Free Pascal == -->
{{omit from|Free Pascal}}
 
=={{header|FreeBASIC}}==
FreeBASIC does not have numeric separator syntax.
Not allow the use of the underscore _ as a digit separator.
 
However, you could, for example, define a macro to remove underscores.
<syntaxhighlight lang="freebasic">Function Remove(Byval Text As String, Char As String="_") As String
Dim As Long i
For n As Long = 0 To Len(Text)-1
If Text[n] <> Asc(char) Then Text[i] = Text[n]: i += 1
Next n
Return Left(Text,i)
End Function
 
#macro __(t,b...)
Vallng(Remove(#t,b))
#endmacro
 
Print __(1_234_567)
Print __(&h__D__E__A__D__B__E__E__F)
Print __(&hFF_BB_00_00 Or &h_FFBB_0000)
Print __(&b_0101_0001_1110_0000)
Print __(26-10-48,"-") 'not a dash
Print Hex(__(&hFF_BB_00_01) Or __(&h_FFBB_0010))</syntaxhighlight>
{{out}}
<pre> 1234567
3735928559
4290445312
20960
261048
FFBB0011</pre>
 
=={{header|Go}}==
Line 128 ⟶ 268:
 
Using the Raku examples plus a few more which Go allows:
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 144 ⟶ 284:
// none of these compile
// floats2 := []float64{_1234.25, 1234_.25, 1234._25, 1234.25_, 12__23.25}
}</langsyntaxhighlight>
 
{{out}}
Line 155 ⟶ 295:
Underscores have to be located within digits. The number of underscores and their position is not restricted.
 
<langsyntaxhighlight lang="java">
 
public class NumericSeparatorSyntax {
Line 186 ⟶ 326:
 
}
</syntaxhighlight>
</lang>
 
{{out}}
Line 197 ⟶ 337:
Underscores allowed in floating point number: 0.0014400
Underscores allowed in floating point exponent: 0.0000144
</pre>
 
=={{header|jq}}==
'''Works with jq and gojq, the C and Go implementations of jq'''
 
jq does not support any separator syntax for numbers, and does not provide any built-in
filters for formatting them with a thousands-separator, or for "decommatizing" strings
that could be interpreted as numbers.
 
The following definitions, however, can be used to commatize integers, whether
expressed as strings or as (JSON) numbers. Exponential notation is
supported, as illustrated by some of the examples below.
 
Note that since both gojq and sufficiently recent versions of jq support indefinitely large
numeric integers, some of the examples assume such support.
<syntaxhighlight lang=jq>
# The def of _nwise/1 can be omitted if using the C implementation of jq.
def _nwise($n):
def n: if length <= $n then . else .[0:$n] , (.[$n:] | n) end;
n;
 
# commatize/0 and commatize/1 are intended for integers or integer-valued strings,
# where integers of the form [-]?[0-9]*[Ee][+]?[0-9]+ are allowed.
# Notice that a leading '+' is disallowed, as is an exponent of the form '-0'.
# Output: a string
def commatize($comma):
def c: [explode | reverse | _nwise(3) | reverse | implode] | reverse | join($comma);
def e: "unable to commatize: " + tostring | error;
 
if type == "string"
then if test("^[0-9]+$") then c
elif test("^-[0-9]+$") then "-" + .[1:] | c
else (capture("(?<s>[-])?(?<i>[0-9]*)[Ee][+]?(?<e>[0-9]+)$") // null)
| if .
then if .i == "" then .i="1" else . end
| .s |= (if . = null then "" else . end)
| .s + ((.i + (.e|tonumber) * "0") | c)
else e
end
end
elif type == "number" and . == floor
then if . >= 0
then tostring|commatize($comma)
else "-" + (-. | tostring | commatize($comma) )
end
else e
end;
 
def commatize:
commatize(",");
</syntaxhighlight>
'''Examples'''
<syntaxhighlight lang=jq>
[ 1e6, 1e9, 123456789, -123456789012, 1e20, "e20", -10e19, 123456789123456789123456789 ] as $nums
| [",", ".", " ", "*"] as $seps
| range(0;$nums|length) as $i
| $nums[$i] | commatize($seps[$i] // ",")
</syntaxhighlight>
{{output}}
<pre>
1,000,000
1.000.000.000
123 456 789
-123*456*789*012
100,000,000,000,000,000,000
100,000,000,000,000,000,000
-100,000,000,000,000,000,000
123,456,789,123,456,789,123,456,789
</pre>
 
Line 204 ⟶ 412:
Commas are not allowed in numeric literals.
 
<langsyntaxhighlight lang="julia">
julia> 2_9
29
Line 236 ⟶ 444:
julia> 10__000__000
ERROR: UndefVarError: __000__000 not defined
</syntaxhighlight>
</lang>
 
=={{header|Nim}}==
Nim allows to use underscores <code>_</code> in numeric literals. An underscore must be preceded and followed by a digit, which means that it cannot be placed at start or end of a literal and that consecutive underscores are forbidden.
 
Using Julia examples, for instance in "inim" REPL:
 
<syntaxhighlight lang="nim">
const a = 2_9 # 29
const b = 2_9_9_0 # 2990
const c = 2_9_9.0_01 # 299.001
const d = 1._01 # Error: invalid token: _ (\95)
const e = -1_0 # -10
const f = -_10 # Error: invalid token: _ (\95)
const g = 0x34_ff # 0x34ff
const h = 0x_34ff # Error: invalid number: '0x_34ff'
const i = 10_000_000 # 10000000
const j = 10__000__000 # Error: only single underscores may occur in a token and token may not end with an underscore: e.g. '1__1' and '1_' are invalid
</syntaxhighlight>
 
=={{header|OCaml}}==
Underscores can be used as separators in integer or floating-point literals, and they are ignored. Underscores can be in any position except at the beginning, and you can use consecutive underscores.
<langsyntaxhighlight lang="ocaml">Printf.printf "%d\n" 1_2_3;; (* 123 *)
Printf.printf "%d\n" 0b1_0_1_0_1;; (* 21 *)
Printf.printf "%d\n" 0xa_bc_d;; (* 43981 *)
Line 249 ⟶ 475:
Printf.printf "%f\n" 1234_.25;; (* 1234.250000 *)
Printf.printf "%f\n" 1234._25;; (* 1234.250000 *)
Printf.printf "%f\n" 1234.25_;; (* 1234.250000 *)</langsyntaxhighlight>
 
=={{header|Pascal}}==
Works with FPC (currently only version 3.3.1).
 
An underscore can be used as a digit separator. This is by default in {$mode delphi}, in other modes it is activated using the {$modeswitch underscoreisseparator}.
<syntaxhighlight lang="pascal">
program test;
{$mode fpc}
{$modeswitch underscoreisseparator}
begin
WriteLn(%1001_1001);
WriteLn(&121_102);
WriteLn(-1_123_123);
WriteLn($1_123_123);
WriteLn(-1_123___123.000_000);
WriteLn(1_123_123.000_000e1_2);
end.
</syntaxhighlight>
 
=={{header|Perl}}==
Perl allows underscore as a grouping / separator character in numeric inputs, as long as you use it between digits, and you do not use two underscores in a row:
<langsyntaxhighlight lang="perl"># Int
print 1_2_3, "\n"; # 123
 
Line 267 ⟶ 511:
 
# Num
print 6.0_22e4, "\n"; # 60220</langsyntaxhighlight>
 
=={{header|Phix}}==
Phix simply ignores underscores in numeric literals, however a leading underscore signifies a normal identifier, much like a123 or tmp2.<br>
Commas are not allowed in numeric literals, since they delimit sequence elements, routine parameters, and such like, for example {1,2,3,4}.
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>? 1_2_3 -- 123
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
--? _1234.25 -- undefined identifier _1234
<span style="color: #0000FF;">?</span> <span style="color: #000000;">1_2_3</span> <span style="color: #000080;font-style:italic;">-- 123
? 0b1_0_1_0_1 -- 21
--? _1234.25 -- undefined identifier _1234</span>
? 0b_1_0_1_0_1 -- 21
<span style="color: #0000FF;">?</span> <span style="color: #000000;">0b1_0_1_0_1</span> <span style="color: #000080;font-style:italic;">-- 21</span>
? 0xa_bc_d -- 43981
<span style="color: #0000FF;">?</span> <span style="color: #000000;">0b_1_0_1_0_1</span> <span style="color: #000080;font-style:italic;">-- 21</span>
? #_DEAD_BEEF_ -- 3735928559.0
<span style="color: #0000FF;">?</span> <span style="color: #000000;">0xa_bc_d</span> <span style="color: #000080;font-style:italic;">-- 43981</span>
? 0x_dead_beef -- 3735928559.0
<span style="color: #0000FF;">?</span> <span style="color: #000000;">#_DEAD_BEEF_</span> <span style="color: #000080;font-style:italic;">-- 3735928559.0</span>
? 3.14_15_93 -- 3.141593
<span style="color: #0000FF;">?</span> <span style="color: #000000;">0x_dead_beef</span> <span style="color: #000080;font-style:italic;">-- 3735928559.0</span>
? 1_2_3_4.2_5 -- 1234.25
<span style="color: #0000FF;">?</span> <span style="color: #000000;">3.14_15_93</span> <span style="color: #000080;font-style:italic;">-- 3.141593</span>
? 1234_.25 -- 1234.25
<span style="color: #0000FF;">?</span> <span style="color: #000000;">1_2_3_4.2_5</span> <span style="color: #000080;font-style:italic;">-- 1234.25</span>
? 1234._25 -- 1234.25
<span style="color: #0000FF;">?</span> <span style="color: #000000;">1234_.25</span> <span style="color: #000080;font-style:italic;">-- 1234.25</span>
? 1234.25_ -- 1234.25
<span style="color: #0000FF;">?</span> <span style="color: #000000;">1234._25</span> <span style="color: #000080;font-style:italic;">-- 1234.25</span>
? 12__34.25 -- 1234.25
<span style="color: #0000FF;">?</span> <span style="color: #000000;">1234.25_</span> <span style="color: #000080;font-style:italic;">-- 1234.25</span>
? 6.0_22e4 -- 60220</lang>
<span style="color: #0000FF;">?</span> <span style="color: #000000;">12__34.25</span> <span style="color: #000080;font-style:italic;">-- 1234.25</span>
<span style="color: #0000FF;">?</span> <span style="color: #000000;">6.0_22e4</span> <span style="color: #000080;font-style:italic;">-- 60220</span>
<!--</syntaxhighlight>-->
 
=={{header|Python}}==
Line 296 ⟶ 543:
 
* E.g. 100_000_000_000, 0x_dead_beef, 3.14_15_93
 
=={{header|Quackery}}==
 
Quackery does not have numeric separator syntax. However, as the compiler is extensible one could, for example, define a builder (i.e. a compiler directive) <code>n</code> to strip commas from the space delimited number following it in the Quackscript.
 
<syntaxhighlight lang="quackery"> [ nextword
[] swap witheach
[ dup char , = iff
drop else join ]
swap join ] builds n ( [ $ --> $ [ )</syntaxhighlight>
 
{{out}}
 
Testing in the Quackery shell.
 
<pre>/O> [ nextword
... [] swap witheach
... [ dup char , = iff
... drop else join ]
... swap join ] builds n ( [ $ --> [ $ )
...
 
Stack empty.
 
/O> n 123,456,789
...
 
Stack: 123456789
 
/O></pre>
 
 
=={{header|Racket}}==
Line 301 ⟶ 579:
Vanilla Racket does not have numeric separator syntax. However, it can be defined by users. A quick solution is to use <code>#%top</code>:
 
<langsyntaxhighlight lang="racket">#lang racket
 
(require syntax/parse/define
Line 316 ⟶ 594:
 
1_234_567.89
1_234__567.89</langsyntaxhighlight>
 
{{out}}
Line 330 ⟶ 608:
If we wish to, for example, disallow multiple consecutive separators like <code>1_234__567.89</code>, we could do so easily:
 
<langsyntaxhighlight lang="racket">#lang racket
 
(require syntax/parse/define
Line 347 ⟶ 625:
 
1_234_567.89
1_234__567.89</langsyntaxhighlight>
 
{{out}}
Line 360 ⟶ 638:
Raku allows underscore as a grouping / separator character in numeric inputs, though there are a few restrictions.
<syntaxhighlight lang="raku" perl6line># Any numeric input value may use an underscore as a grouping/separator character.
# May occur in nearly any position, in any* number. * See restrictions below.
 
Line 386 ⟶ 664:
# say 1234._25;
# say 1234.25_;
# say 12__34.25;</langsyntaxhighlight>
 
=={{header|REXX}}==
Line 420 ⟶ 698:
 
There is a way to work around such that blanks or commas could be used within a REXX program with a bit of coding:
<langsyntaxhighlight lang="rexx">pi= 3 . 14159 26535 89793 23846 26433 83279 50288 41971 69399 37510 58209 74945
pi= 3 . 14159_26535_89793_23846_26433_83279_50288_41971_69399_37510_58209_74945
pi = space( translate(pi, , ",_"), 0)</langsyntaxhighlight>
 
─── where the last REXX statement will translate (change) any number of separator characters into blanks, &nbsp; and
Line 434 ⟶ 712:
''"The digits of a numeric literal may be separated by arbitrarily many underscores for purposes of legibility."''
Let's see how its work in a Scala REPL session:
<langsyntaxhighlight Scalalang="scala">Welcome to Scala 2.13.0 (Java HotSpot(TM) 64-Bit Server VM, Java 12.0.2).
Type in expressions for evaluation. Or try :help.
 
Line 459 ⟶ 737:
res5: Double = 1234.25
 
scala></langsyntaxhighlight>
 
=={{header|Sidef}}==
Sidef allows underscores as a separator character in numeric inputs.
<langsyntaxhighlight lang="ruby"># Int
say 1_2_3; # 123
 
Line 484 ⟶ 762:
say 1234.25_; # 1234.25
say 12__34.25; # 1234.25
# say _1234.25; # syntax error</langsyntaxhighlight>
 
=={{header|V (Vlang)}}==
Vlang also supports writing numbers with _ as a separator.
<syntaxhighlight lang="Rust">
fn main() {
numbers := [1_000_000, 2_882, 3_122, 0b1_0_0_0_1, 0xa_bc_d]
for number in numbers {println(number)}
}
</syntaxhighlight>
 
{{out}}
<pre>
1000000
2882
3122
17
43981
</pre>
 
=={{header|XPL0}}==
Line 492 ⟶ 788:
program are also ignored.
 
<langsyntaxhighlight XPL0lang="xpl0">def Meg = 1_000_000;
[IntOut(0, Meg); CrLf(0);
RlOut(0, 123__45.67_89_); CrLf(0);
Line 498 ⟶ 794:
HexOut(0, %1010_1011_1100_1101_1110_1111_0000_0001); CrLf(0);
IntOut(0, IntIn(0));
]</langsyntaxhighlight>
 
{{out}}
Line 513 ⟶ 809:
{{libheader|Wren-fmt}}
Consistent with its C heritage, Wren doesn't support any form of separator in numeric literals.
However, it's possible using the MathWren-fmt module to add any single character 'thousands' separator when 'stringifying' an integer as the example below shows.
 
As currently written, this just supports separation of decimal integers into 3 digit groups from the right though it could be extended to deal with other scenarios as well.
<langsyntaxhighlight ecmascriptlang="wren">import "./fmt" for Fmt
 
var nums = [1e6, 1e9, 123456789, -123456789012]
var seps = [",", ".", " ", "*"]
for (i in 0...nums.count) System.print(Fmt.commatize(nums[i], seps[i]))</langsyntaxhighlight>
 
{{out}}
Line 531 ⟶ 827:
 
=={{header|zkl}}==
<langsyntaxhighlight lang="zkl">For source code, integers and floats allow a "_" between digits (or trailing)
and completely ignores them:
1_000 == 1_000_ == 1_0_0_0 == 1__________000
1_2.3_4 == 12.34
For hex, both "_" and "|" are allowed: 0x12|34</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">For printing, the String.fmt method will add separators for %d (interger: ","),
%f (float: ","), %x (hex: "|") and %2B (binary: "|").
"%,d %,.0f %,x %,.2B".fmt(1234, 1234.0, 0x1234, 0x1234).println();
--> "1,234 1,234 12|34 1|0010|0011|0100"
Each objects toString method has optional parameters to specify a separator
and "column width". This method is called (by fmt) for the above tags.</langsyntaxhighlight>
9,476

edits