-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtext.fixedlength.fmfn
More file actions
83 lines (62 loc) · 2.51 KB
/
text.fixedlength.fmfn
File metadata and controls
83 lines (62 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
Purpose: Fixed length text, padded and aligned.
Returns: Text
Name: text.fixedLength ( theText; thePaddingChar; theAlignment; theFixedLength )
Parameters: ( theText ) text
( thePaddingChar ) char
( theAlignment ) = enum (left, right, center)
( theFixedLength ) number
Dependencies: NONE
References: NONE
NOTE: If an invalid or no alignment is provided, the function defaults to left alignment.
2013-10-30, JPS, Created.
*/
Let (
[
//Get the text length
~theTextLength = Length ( theText );
//Get the padding length
~thePaddingCharLength = Length ( thePaddingChar );
//Determine the alignment
~textAlignment =
Case (
theAlignment = "Right" or theAlignment = "R" or theAlignment = 1;
"right";
theAlignment = "Center" or theAlignment = "C" or theAlignment = 0;
"center";
//DEFAULT
"left"
);
//For center alignment we need to calculate the padding on the left and right
~adjustedLength = theFixedLength - ~theTextLength;
~lengthLeft = Max ( Ceiling ( ~adjustedLength / 2); 0 );
~lengthRight = Max ( Floor ( ~adjustedLength / 2); 0 )
];
Case (
//If the fix length may only be a number
Length ( theFixedlength ) <> Length ( GetAsNumber ( theFixedlength ));
"?";
//If theFixedLength = 0, return ""
theFixedLength = 0;
"";
//If the text is longer than theFixedlength return error ?
~theTextLength > GetAsNumber ( theFixedlength );
"?";
//If thePaddingChar is more than one character return error ?
~thePaddingCharLength > 1;
"?";
//If the text length = theFixedLength, or the paddingChar is empty, return theText
~theTextLength = theFixedLength or IsEmpty ( thePaddingChar );
theText;
//Process left aligned
~textAlignment = "left";
text.fixedLength ( theText & thePaddingChar; thePaddingChar; ~textAlignment; theFixedlength );
//Process right aligned
~textAlignment = "right";
text.fixedLength ( thePaddingChar & theText; thePaddingChar; ~textAlignment; theFixedlength );
//Process center aligned
text.fixedLength ( ""; thePaddingChar; "left"; ~lengthLeft ) &
theText &
text.fixedLength ( ""; thePaddingChar; "right"; ~lengthRight )
)
)