Numeric separator syntax: Difference between revisions

m
m (syntax highlighting fixup automation)
m (→‎{{header|Wren}}: Minor tidy)
 
(6 intermediate revisions by 5 users not shown)
Line 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">
#include <locale.h>
Line 135:
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}}==
Line 302 ⟶ 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 373 ⟶ 476:
Printf.printf "%f\n" 1234._25;; (* 1234.250000 *)
Printf.printf "%f\n" 1234.25_;; (* 1234.250000 *)</syntaxhighlight>
 
=={{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}}==
Line 642 ⟶ 763:
say 12__34.25; # 1234.25
# say _1234.25; # syntax error</syntaxhighlight>
 
=={{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 673 ⟶ 812:
 
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.
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
 
var nums = [1e6, 1e9, 123456789, -123456789012]
9,482

edits