Sign in Join
Wiki copytemporary

BcdUnit: what changed

Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11## Welcome
22
33Welcome to information page about FMTBcd. Goal of this page is to contain information needed for implementing FPC version of the FMTBcd.
44
55------------------------------------------------------------------------
66
77FMTBcd unit defines a number of binary-coded decimal (BCD) routines:
88
99## Types
1010
11-`TBcd  = packed record`\
12-`   Precision: Byte;`\
13-`   SignSpecialPlaces: Byte;`\
14-`   Fraction: packed array [0..31] of Byte;`\
15-` end;`
11+``` pascal
12+TBcd = packed record
13+ Precision: Byte;
14+ SignSpecialPlaces: Byte;
15+ Fraction: packed array [0..31] of Byte;
16+ end;
17+```
1618
17-`PBcd: pointer to TBcd`
19+```
20+PBcd: pointer to TBcd
21+```
1822
1923### Precision
2024
2125Precision states how many decimal digits are valid out of the 64 nibbles available for use.
2226
2327### SignSpecialPlaces
2428
2529SignSpecialPlaces is three fields packed into a byte.
2630
2731- The first bit is the sign (bit value 128).
2832- The second bit is 'Special' (So special, I can't work out what it's for).
2933- The third - eights bits contain the number of decimals (less the number of default digits???). 6 bits gives us up to 32 digits.
3034
3135### Fraction
3236
3337array of BCD Nibbles, 00..99 per Byte, high Nibble 1st
3438
3539Can anybody correct the following test cases?
3640
3741
3842
3943What do you mean by 'correct'?
4044
4145## test cases
4246
4347### integers
4448
4549If you look at a Tbdc record in the debugger, it will display the Precision as an integer, the SignSpecialPlaces as an interger and the Fraction as an array of integers. The fraction parts of the TBCD are really arrays of nibbles - one nibble per digit, two per byte. Following are some 'debug displays' and their translations. There is a space between bytes. So...
4650
4751zero is boring...
4852
49-`      0 -> (Precision: 0;SignSpecialPlaces: 0;  Fraction:(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0))`
53+```
54+ 0 -> (Precision: 0;SignSpecialPlaces: 0; Fraction:(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0))
55+```
5056
51571..3
5258
53-`      1 -> (Precision: 1;SignSpecialPlaces: 0;  Fraction:(16,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0))`\
54-`      1 -> (Precision: 1;SignSpecialPlaces: 0;  Fraction:(1,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0,...`
59+```
60+ 1 -> (Precision: 1;SignSpecialPlaces: 0; Fraction:(16,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0))
61+ 1 -> (Precision: 1;SignSpecialPlaces: 0; Fraction:(1,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0,...
62+```
5563
56-`      2 -> (Precision: 1;SignSpecialPlaces: 0;  Fraction:(32,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0))`\
57-`      2 -> (Precision: 1;SignSpecialPlaces: 0;  Fraction:(2,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0,...`
64+```
65+ 2 -> (Precision: 1;SignSpecialPlaces: 0; Fraction:(32,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0))
66+ 2 -> (Precision: 1;SignSpecialPlaces: 0; Fraction:(2,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0,...
67+```
5868
59-`      3 -> (Precision: 1;SignSpecialPlaces: 0;  Fraction:(48,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0))`\
60-`      3 -> (Precision: 1;SignSpecialPlaces: 0;  Fraction:(3,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0,...`
69+```
70+ 3 -> (Precision: 1;SignSpecialPlaces: 0; Fraction:(48,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0))
71+ 3 -> (Precision: 1;SignSpecialPlaces: 0; Fraction:(3,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0,...
72+```
6173
62-`etc...`\
63-`      4 -> (Precision: 1;SignSpecialPlaces: 0;  Fraction:(64,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0))`\
64-`      5 -> (Precision: 1;SignSpecialPlaces: 0;  Fraction:(80,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0))`\
65-`      6 -> (Precision: 1;SignSpecialPlaces: 0;  Fraction:(96,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0))`\
66-`      7 -> (Precision: 1;SignSpecialPlaces: 0;  Fraction:(112,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0))`\
67-`      8 -> (Precision: 1;SignSpecialPlaces: 0;  Fraction:(128,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0))`\
68-`      9 -> (Precision: 1;SignSpecialPlaces: 0;  Fraction:(144,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0))`
74+```
75+etc...
76+ 4 -> (Precision: 1;SignSpecialPlaces: 0; Fraction:(64,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0))
77+ 5 -> (Precision: 1;SignSpecialPlaces: 0; Fraction:(80,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0))
78+ 6 -> (Precision: 1;SignSpecialPlaces: 0; Fraction:(96,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0))
79+ 7 -> (Precision: 1;SignSpecialPlaces: 0; Fraction:(112,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0))
80+ 8 -> (Precision: 1;SignSpecialPlaces: 0; Fraction:(128,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0))
81+ 9 -> (Precision: 1;SignSpecialPlaces: 0; Fraction:(144,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0))
82+```
6983
708410 - note: the fractional part is the same as for 1, the precision is now 2. i.e. 10 = 1 * 10^2
7185
72-`     10 -> (Precision: 2;SignSpecialPlaces: 0;  Fraction:(16,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0))`\
73-`     10 -> (Precision: 2;SignSpecialPlaces: 0;  Fraction:(1,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0,...`
86+```
87+ 10 -> (Precision: 2;SignSpecialPlaces: 0; Fraction:(16,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0))
88+ 10 -> (Precision: 2;SignSpecialPlaces: 0; Fraction:(1,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0,...
89+```
7490
7591100 - note: the fractional part is the same as for 1, the precision is now 3. i.e. 10 = 1 * 10^3
7692
77-`    100 -> (Precision: 3;SignSpecialPlaces: 0;  Fraction:(16,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0))`\
78-`    100 -> (Precision: 2;SignSpecialPlaces: 0;  Fraction:(1,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0,...`\
79-`etc`\
80-`   1000 -> (Precision: 4;SignSpecialPlaces: 0;  Fraction:(16,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0))`\
81-`   1000 -> (Precision: 4;SignSpecialPlaces: 0;  Fraction:(1,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0,...`
93+```
94+ 100 -> (Precision: 3;SignSpecialPlaces: 0; Fraction:(16,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0))
95+ 100 -> (Precision: 2;SignSpecialPlaces: 0; Fraction:(1,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0,...
96+etc
97+ 1000 -> (Precision: 4;SignSpecialPlaces: 0; Fraction:(16,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0))
98+ 1000 -> (Precision: 4;SignSpecialPlaces: 0; Fraction:(1,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0,...
99+```
82100
83101some more interesting cases:
84102
85-`   1111 -> (Precision: 4;SignSpecialPlaces: 0;  Fraction:(17,17,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0))`\
86-`   1111 -> (Precision: 4;SignSpecialPlaces: 0;  Fraction:(1,1, 1,1, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0,...`
103+```
104+ 1111 -> (Precision: 4;SignSpecialPlaces: 0; Fraction:(17,17,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0))
105+ 1111 -> (Precision: 4;SignSpecialPlaces: 0; Fraction:(1,1, 1,1, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0,...
106+```
87107
88-`   1234 -> (Precision: 4;SignSpecialPlaces: 0;  Fraction:(18,52,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0))`\
89-`   1234 -> (Precision: 4;SignSpecialPlaces: 0;  Fraction:(1,2, 3,4, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0,...`
108+```
109+ 1234 -> (Precision: 4;SignSpecialPlaces: 0; Fraction:(18,52,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0))
110+ 1234 -> (Precision: 4;SignSpecialPlaces: 0; Fraction:(1,2, 3,4, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0,...
111+```
90112
91-`     -5 -> (Precision: 1;SignSpecialPlaces: 128;Fraction:(80,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0))`\
92-`     -5 -> (Precision: 1;SignSpecialPlaces: 128;Fraction:(5,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0,... `
113+```
114+ -5 -> (Precision: 1;SignSpecialPlaces: 128;Fraction:(80,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0))
115+ -5 -> (Precision: 1;SignSpecialPlaces: 128;Fraction:(5,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0,...
116+```
93117
94-`     -6 -> (Precision: 1;SignSpecialPlaces: 128;Fraction:(96,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0))`\
95-`     -6 -> (Precision: 1;SignSpecialPlaces: 128;Fraction:(6,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0,... `
118+```
119+ -6 -> (Precision: 1;SignSpecialPlaces: 128;Fraction:(96,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0))
120+ -6 -> (Precision: 1;SignSpecialPlaces: 128;Fraction:(6,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0,...
121+```
96122
97-`   9999 -> (Precision: 4;SignSpecialPlaces: 0;  Fraction:(153,153,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0))`\
98-`   9999 -> (Precision: 4;SignSpecialPlaces: 0;  Fraction:(9,9, 9,9, 9,9, 9,9, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0,... `
123+```
124+ 9999 -> (Precision: 4;SignSpecialPlaces: 0; Fraction:(153,153,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0))
125+ 9999 -> (Precision: 4;SignSpecialPlaces: 0; Fraction:(9,9, 9,9, 9,9, 9,9, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0,...
126+```
99127
100-`  -9999 -> (Precision: 4;SignSpecialPlaces: 128;Fraction:(153,153,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0))`\
101-`  -9999 -> (Precision: 4;SignSpecialPlaces: 128;  Fraction:(9,9, 9,9, 9,9, 9,9, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0,... `
128+```
129+ -9999 -> (Precision: 4;SignSpecialPlaces: 128;Fraction:(153,153,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0))
130+ -9999 -> (Precision: 4;SignSpecialPlaces: 128; Fraction:(9,9, 9,9, 9,9, 9,9, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0,...
131+```
102132
103133Displaying the structure in HEX helps...(you should never see any hex digits in the fraction part - only 0..9 is used)
104134
105-`  32767 -> (Precision: 5;SignSpecialPlaces: 0;  Fraction:($32,$76,$70,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0))`\
106-`  32767 -> (Precision: 5;SignSpecialPlaces: 0;  Fraction:(3,2, 7,6, 70,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0,...`
135+```
136+ 32767 -> (Precision: 5;SignSpecialPlaces: 0; Fraction:($32,$76,$70,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0))
137+ 32767 -> (Precision: 5;SignSpecialPlaces: 0; Fraction:(3,2, 7,6, 70,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0,...
138+```
107139
108-` -32768 -> (Precision: 5;SignSpecialPlaces: 128;Fraction:($32,$76,$80,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0))`\
109-` -32768 -> (Precision: 5;SignSpecialPlaces: 128;Fraction:(3,2, 7,6, 80,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0,...`
140+```
141+ -32768 -> (Precision: 5;SignSpecialPlaces: 128;Fraction:($32,$76,$80,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0))
142+ -32768 -> (Precision: 5;SignSpecialPlaces: 128;Fraction:(3,2, 7,6, 80,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0,...
143+```
110144
111-`  2147483647 -> (Precision:10;SignSpecialPlaces: 0;  Fraction:($21,$47,$48,$36,$47,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0))`\
112-`  2147483647 -> (Precision:10;SignSpecialPlaces: 0;  Fraction:(2,1, 4,7, 4,8, 3,6, 4,7, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0,...`
145+```
146+ 2147483647 -> (Precision:10;SignSpecialPlaces: 0; Fraction:($21,$47,$48,$36,$47,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0))
147+ 2147483647 -> (Precision:10;SignSpecialPlaces: 0; Fraction:(2,1, 4,7, 4,8, 3,6, 4,7, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0,...
148+```
113149
114-` -2147483647 -> (Precision:10;SignSpecialPlaces: 128;Fraction:($21,$47,$48,$36,$47,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0))`\
115-` -2147483647 -> (Precision:10;SignSpecialPlaces: 128;Fraction:(2,1, 4,7, 4,8, 3,6, 4,7, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0,...`
150+```
151+ -2147483647 -> (Precision:10;SignSpecialPlaces: 128;Fraction:($21,$47,$48,$36,$47,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0))
152+ -2147483647 -> (Precision:10;SignSpecialPlaces: 128;Fraction:(2,1, 4,7, 4,8, 3,6, 4,7, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0,...
153+```
116154
117155### floats
118156
119-`  -0.01234567 ->  (Precision:8; SignSpecialPlaces: 136 {$80+8} Fraction: ( 1,35,69,103, ... 0,0)`
157+```
158+ -0.01234567 -> (Precision:8; SignSpecialPlaces: 136 {$80+8} Fraction: ( 1,35,69,103, ... 0,0)
159+```
120160
121161## Const
122162
123-` NullBcd: TBcd = (Precision: 0; SignSpecialPlaces: 0; Fraction:(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0));`
163+```
164+ NullBcd: TBcd = (Precision: 0; SignSpecialPlaces: 0; Fraction:(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0));
165+```
124166
125167it's a zero (according to Delphi documentation: "Use NullBcd to indicate an unknown or missing TBcd value. NullBcd is a TBcd with all its fields set to 0. ")
126168
127169## Exceptions
128170
129-` EBcdException, EBcdOverflowException`
171+```
172+ EBcdException, EBcdOverflowException
173+```
130174
131175??
132176
133-`  EBcdException = Class(Exception);`\
134-`  EBcdOverflowException = Class(Exception);`
177+```
178+ EBcdException = Class(Exception);
179+ EBcdOverflowException = Class(Exception);
180+```
135181
136182## Utility
137183
138-` function BcdPrecision(const Bcd: TBcd): Word;`\
139-` function BcdScale(const Bcd: TBcd): Word;`\
140-` function IsBcdNegative(const Bcd: TBcd): Boolean;`
184+``` pascal
185+ function BcdPrecision(const Bcd: TBcd): Word;
186+ function BcdScale(const Bcd: TBcd): Word;
187+ function IsBcdNegative(const Bcd: TBcd): Boolean;
188+```
141189
142190??
143191
144192## Arithmetic
145193
146-` procedure BcdAdd(const bcdIn1, bcdIn2: TBcd; var bcdOut: TBcd);`\
147-` procedure BcdSubtract(const bcdIn1, bcdIn2: TBcd; var bcdOut: TBcd);`\
148-` function NormalizeBcd(const InBcd: TBcd; var OutBcd: TBcd; const Prec, Scale: Word): Boolean;`
194+``` pascal
195+ procedure BcdAdd(const bcdIn1, bcdIn2: TBcd; var bcdOut: TBcd);
196+ procedure BcdSubtract(const bcdIn1, bcdIn2: TBcd; var bcdOut: TBcd);
197+ function NormalizeBcd(const InBcd: TBcd; var OutBcd: TBcd; const Prec, Scale: Word): Boolean;
198+```
149199
150200Returns True if successful, False if Int Digits needed to be truncated
151201
152-` procedure BcdMultiply(const bcdIn1, bcdIn2: TBcd; var bcdOut: TBcd); overload;`\
153-` procedure BcdMultiply(const bcdIn: TBcd; const DoubleIn: Double; var bcdOut: TBcd); overload;`\
154-` procedure BcdMultiply(const bcdIn: TBcd; const StringIn: string; var bcdOut: TBcd); overload;`\
155-` procedure BcdMultiply(StringIn1, StringIn2: string; var bcdOut: TBcd); overload;`\
156-` procedure BcdDivide(Dividend, Divisor: string; var bcdOut: TBcd); overload;`\
157-` procedure BcdDivide(const Dividend, Divisor: TBcd; var bcdOut: TBcd); overload;`\
158-` procedure BcdDivide(const Dividend: TBcd; const Divisor: Double; var bcdOut: TBcd); overload;`\
159-` procedure BcdDivide(const Dividend: TBcd; const Divisor: string; var bcdOut: TBcd); overload;`
202+``` pascal
203+ procedure BcdMultiply(const bcdIn1, bcdIn2: TBcd; var bcdOut: TBcd); overload;
204+ procedure BcdMultiply(const bcdIn: TBcd; const DoubleIn: Double; var bcdOut: TBcd); overload;
205+ procedure BcdMultiply(const bcdIn: TBcd; const StringIn: string; var bcdOut: TBcd); overload;
206+ procedure BcdMultiply(StringIn1, StringIn2: string; var bcdOut: TBcd); overload;
207+ procedure BcdDivide(Dividend, Divisor: string; var bcdOut: TBcd); overload;
208+ procedure BcdDivide(const Dividend, Divisor: TBcd; var bcdOut: TBcd); overload;
209+ procedure BcdDivide(const Dividend: TBcd; const Divisor: Double; var bcdOut: TBcd); overload;
210+ procedure BcdDivide(const Dividend: TBcd; const Divisor: string; var bcdOut: TBcd); overload;
211+```
160212
161213## Creation
162214
163-` procedure VarFMTBcdCreate(var ADest: Variant; const ABcd: TBcd); overload;`\
164-` function VarFMTBcdCreate: Variant; overload;`\
165-` function VarFMTBcdCreate(const AValue: string; Precision, Scale: Word):Variant; overload;`\
166-` function VarFMTBcdCreate(const AValue: Double; Precision: Word; Scale: Word ): Variant; overload;`\
167-` function VarFMTBcdCreate(const ABcd: TBcd): Variant; overload;`\
168-` function VarIsFMTBcd(const AValue: Variant): Boolean; overload;`\
169-` function VarFMTBcd: TVarType;`
215+``` pascal
216+ procedure VarFMTBcdCreate(var ADest: Variant; const ABcd: TBcd); overload;
217+ function VarFMTBcdCreate: Variant; overload;
218+ function VarFMTBcdCreate(const AValue: string; Precision, Scale: Word):Variant; overload;
219+ function VarFMTBcdCreate(const AValue: Double; Precision: Word; Scale: Word ): Variant; overload;
220+ function VarFMTBcdCreate(const ABcd: TBcd): Variant; overload;
221+ function VarIsFMTBcd(const AValue: Variant): Boolean; overload;
222+ function VarFMTBcd: TVarType;
223+```
170224
171225## Conversions
172226
173-` StrToBcd, TryStrToBcd, DoubleToBcd, DoubleToBcd, IntegerToBcd,`\
174-` VarToBcd, CurrToBCD, BcdToStr, BcdToDouble, BcdToInteger,`\
175-` BCDToCurr, BcdToStrF, FormatBcd, BcdCompare`
227+```
228+ StrToBcd, TryStrToBcd, DoubleToBcd, DoubleToBcd, IntegerToBcd,
229+ VarToBcd, CurrToBCD, BcdToStr, BcdToDouble, BcdToInteger,
230+ BCDToCurr, BcdToStrF, FormatBcd, BcdCompare
231+```
176232
177233### to BCD
178234
179235Convert String/Double/Integer to BCD struct
180236
181-` function StrToBcd(const AValue: string): TBcd;`\
182-` function TryStrToBcd(const AValue: string; var Bcd: TBcd): Boolean;`\
183-` function DoubleToBcd(const AValue: Double): TBcd; overload;`\
184-` procedure DoubleToBcd(const AValue: Double; var bcd: TBcd); overload;`\
185-` function IntegerToBcd(const AValue: Longint): TBcd;`\
186-` function VarToBcd(const AValue: Variant): TBcd;`\
187-` function CurrToBCD(const Curr: Currency; var BCD: TBcd; Precision: Integer; Decimals: Integer): Boolean;`\
188-` `
237+``` pascal
238+ function StrToBcd(const AValue: string): TBcd;
239+ function TryStrToBcd(const AValue: string; var Bcd: TBcd): Boolean;
240+ function DoubleToBcd(const AValue: Double): TBcd; overload;
241+ procedure DoubleToBcd(const AValue: Double; var bcd: TBcd); overload;
242+ function IntegerToBcd(const AValue: Longint): TBcd;
243+ function VarToBcd(const AValue: Variant): TBcd;
244+ function CurrToBCD(const Curr: Currency; var BCD: TBcd; Precision: Integer; Decimals: Integer): Boolean;
245+```
189246
190247### from BCD
191248
192249Convert Bcd struct to string/Double/Integer
193250
194-` function BcdToStr(const Bcd: TBcd): string; overload;`\
195-` function BcdToDouble(const Bcd: TBcd): Double;`\
196-` function BcdToInteger(const Bcd: TBcd; Truncate: Boolean): Longint;`\
197-` function BCDToCurr(const BCD: TBcd; var Curr: Currency): Boolean;`\
198-` function BcdToStrF(const Bcd: TBcd; Format: TFloatFormat; const Precision, Digits: Integer): string;`
251+``` pascal
252+ function BcdToStr(const Bcd: TBcd): string; overload;
253+ function BcdToDouble(const Bcd: TBcd): Double;
254+ function BcdToInteger(const Bcd: TBcd; Truncate: Boolean): Longint;
255+ function BCDToCurr(const BCD: TBcd; var Curr: Currency): Boolean;
256+ function BcdToStrF(const Bcd: TBcd; Format: TFloatFormat; const Precision, Digits: Integer): string;
257+```
199258
200259Formatting Bcd as string
201260
202-` function FormatBcd(const Format: string; Bcd: TBcd): string;`\
203-` function BcdCompare(const bcd1, bcd2: TBcd): Integer;`
261+``` pascal
262+ function FormatBcd(const Format: string; Bcd: TBcd): string;
263+ function BcdCompare(const bcd1, bcd2: TBcd): Integer;
264+```
204265
205266Warnings: 1. All these functions can accept integer as a parameter, but they produce a wrong result. For converting integers, use
206267
207-` function BCDToInt(Value: Integer):Integer; `
268+``` pascal
269+ function BCDToInt(Value: Integer):Integer;
270+```
208271
209272from the *sysutils* unit.
210273
2112742\. Converting invalid BCD arrays cause exceptions.
212275
213276## Resources
214277
215278- [Additional information](http://homepages.borland.com/efg2lab/Library/Delphi/MathInfo/) available from efg's Computer Lab ([archived](https://web.archive.org/web/20080208085333/http://homepages.borland.com/efg2lab/Library/Delphi/MathInfo/) at archive.org).
216279
217280---
218281*From the Free Pascal wiki, page "BcdUnit", as it stood on 2021-02-17 after a change by Bart, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*
219282Category: FPC
220283Category: Proposals

Back to history