```    1:- module(tidylog_number_decimal, [decimal//1]).    2
3% Define Prolog decimal format
4
5:- use_module(library(tidylog/common), [ parsing//0
6                                       , when_generating//1
7                                       , when_parsing//1
8                                       ]).    9
10
11decimal(N) -->
12    parsing,
13    !,
14    ( "-", decimal_(M), { N is -M }
15    ; decimal_(N)
16    ).
17decimal(N) -->
18    % generating
19    { integer(N) },
20    ( { N < 0 }, "-", { M is -N }, decimal_(M)
21    ; decimal_(N)
22    ).
23
24% positive decimal integer
25decimal_(N) -->
26    when_generating(decimal_codes(N,[Code|Codes])),
27
28    digit(Code),
29    digits(Codes),
30
31    when_parsing(decimal_codes(N,[Code|Codes])).
32
33
34digit(C) -->
35    [C],
36    { between(0'0,0'9,C) }.
37
38
39digits([Sep,Code|Codes]) -->
40    separator(Sep),
41    digit(Code),
42    digits(Codes).
43digits([Code|Codes]) -->
44    digit(Code),
45    digits(Codes).
46digits([]) -->
47    [].```
decimal_codes(?N:integer, ?Code:list) is det
True if integer N, in decimal notation, is represented as list of Codes. When generating Codes from N, underscore characters are inserted every 3 digits.
```   55decimal_codes(N,Codes) :-
56    integer(N),
57    !,
58    format(codes(Codes),"~3I",[N]).
59decimal_codes(N,Codes) :-
60    nonvar(Codes),
61    name(N,Codes),
62    integer(N).
63
64
65separator(C) -->
66    [C],
67    { separator_char(C) }.
68
69
70separator_char(0'_).
71separator_char(0'\s)```