Size: 3526
Comment:
|
← Revision 15 as of 2017-02-16 19:43:29 ⇥
Size: 3315
Comment:
|
Deletions are marked like this. | Additions are marked like this. |
Line 1: | Line 1: |
= Formatting = | #acl AutoAdminGroup:read,write,admin,delete,revert All:read |
Line 3: | Line 3: |
{{{#!html <div class="borderlesstable"> }}} ||<tableclass="borderlesstable"><[[../ErrorHandling|Back]] || [[../TutorialContents|Contents]] || End of Tutorial|| {{{#!html </div> }}} |
|
Line 4: | Line 11: |
The default way in which APL displays results may not always suit your requirements. Obviously you can do a certain amount by using functions like size to reshape data, or catenate to join data items, but for many applications you may want much more sophisticated facilities. You may, for example, want to insert currency signs and spaces in numeric output, or produce a neatly formatted financial report, or specify precisely the format in which numbers are displayed. | = Formatting (14 of 14) = |
Line 6: | Line 13: |
== Formatting == | == Overview == <<TableOfContents>> The default way in which APL displays results may not always suit your requirements. Obviously you can do a certain amount by using functions like size to reshape data, or catenate to join data items, but for many applications you may want much more sophisticated facilities. You may, for example, want to insert currency signs and spaces in numeric output, or produce a neatly formatted financial report, or specify precisely the format in which numbers are displayed. |
Line 8: | Line 22: |
Line 15: | Line 27: |
Line 19: | Line 29: |
Here we give details of three common functions: {{{•}}} ('''Format''' or '''Thorn'''), {{{¸}}} ('''Alpha''') and the APLX system function {{{ŒFMT}}}. They can be used purely to convert numeric data to characters. The converted data looks the same, but has the properties associated with character data. | Here we give details of two common functions: {{{⍕}}} ('''Format''' or '''Thorn''') and the system function {{{⎕FMT}}} available in every APL but APL2. They can be used purely to convert numeric data to characters. The converted data looks the same, but has the properties associated with character data. |
Line 21: | Line 31: |
Additionally, each function lets you specify how many character positions a number should occupy when it's displayed, and how many of these positions are available for decimal places. The number of characters and number of decimal places are specified in the left argument: | Additionally, both functions let you specify how many character positions a number should occupy when it's displayed, and how many of these positions are available for decimal places. The number of characters and number of decimal places are specified in the left argument. == The "Format" primitive == An example without left argument: |
Line 24: | Line 38: |
6 2 • 1341.82921 | ⍕ 0.0000003 3.0123456789 3E¯7 3.012345679 }}} (Note that the result depends on the system variable `⎕PP` = Print Precision) {{{ 6 2 ⍕ 341.82921 |
Line 29: | Line 49: |
{{{•}}} and {{{¸}}} have the optional extra facility of allowing you to use editing characters to define a 'picture' of how data is to look when displayed, known as format-by-example. The picture is the left argument and the data the right. | == The system function ⎕FMT == |
Line 31: | Line 51: |
Here is an example of {{{•}}} used to format by example: | Note that `⎕FMT` is not available in APL2. {{{⎕FMT}}} takes the process a stage further, allowing a variety of picture phrases, qualifiers and decorators to be supplied as the format specification. |
Line 34: | Line 56: |
L „ ' 55 @ $55.50 EA' R „ 3 2½3 4.99 7 7.45 12 .5 L•R 3 @ $ 4.99 EA 7 @ $ 7.45 EA 12 @ $ .50 EA }}} The following example shows the values in a 4-row 2-column matrix called {{{TAB}}}. It then shows the {{{¸}}} function applied to this matrix and its effect on {{{TAB}}}: {{{ TAB 1096.2 ¯416.556 296.974 1085.238 ¯811.188 844.074 ¯745.416 153.468 '$$Z,ZZ9.99 DR ' ¸ TAB $1,096.20 $416.56 DR $296.97 $1,085.24 $811.19 DR $844.07 $745.42 DR $153.47 }}} {{{ŒFMT}}} takes the process a stage further, allowing a variety of picture phrases, qualifiers and decorators to be supplied as the format specification. {{{ 'B K2 G< ZZ9 DOLLARS AND 99 CENTS>' ŒFMT 8.23 12.86 0 2.52 |
'B K2 G< ZZ9 DOLLARS AND 99 CENTS>' ⎕FMT 8.23 12.86 0 2.52 |
Line 62: | Line 59: |
Line 64: | Line 62: |
`⎕FMT` is a very powerful function which allows you to format numbers in all sorts of ways. Not surprisingly it is one of the most complex functions available in modern APLs. == End of Tutorial == That's the end of the APL Tutorial. Congratulations on making it all the way to the end! If you're still hungry for more, you might want to check out [[LearnMoreApl/FurtherTopics|Further Topics in APL]] which covers some more advanced topics. |
|
Line 65: | Line 68: |
< [[LearnApl/ErrorHandling|Back]] [[LearnApl/TutorialContents|Contents]] [[LearnApl/AplClasses|Next]] > CategoryAboutApl CategoryGuides CategoryAplx |
{{{#!html <div class="borderlesstable"> }}} ||<tableclass="borderlesstable"><[[../ErrorHandling|Back]] || [[../TutorialContents|Contents]] || End of Tutorial>|| {{{#!html </div> }}} ---- CategoryAboutApl |
Formatting (14 of 14)
Overview
Contents
The default way in which APL displays results may not always suit your requirements. Obviously you can do a certain amount by using functions like size to reshape data, or catenate to join data items, but for many applications you may want much more sophisticated facilities.
You may, for example, want to insert currency signs and spaces in numeric output, or produce a neatly formatted financial report, or specify precisely the format in which numbers are displayed.
APL includes flexible ways of formatting data. Formatting functions both:
- convert the format of data from numbers to characters
- allow you to specify how (converted) numeric data will be laid out.
The formatting functions available will depend on which APL interpreter you are using, so the following is only a guide. You will need to check your APL's documentation for available formatting functions.
Here we give details of two common functions: ⍕ (Format or Thorn) and the system function ⎕FMT available in every APL but APL2. They can be used purely to convert numeric data to characters. The converted data looks the same, but has the properties associated with character data.
Additionally, both functions let you specify how many character positions a number should occupy when it's displayed, and how many of these positions are available for decimal places. The number of characters and number of decimal places are specified in the left argument.
The "Format" primitive
An example without left argument:
⍕ 0.0000003 3.0123456789 3E¯7 3.012345679
(Note that the result depends on the system variable ⎕PP = Print Precision)
6 2 ⍕ 341.82921 341.83
(Note that since the number had to be truncated to fit the character positions allowed, it was first rounded to make the truncated representation as accurate as possible.)
The system function ⎕FMT
Note that ⎕FMT is not available in APL2.
⎕FMT takes the process a stage further, allowing a variety of picture phrases, qualifiers and decorators to be supplied as the format specification.
'B K2 G< ZZ9 DOLLARS AND 99 CENTS>' ⎕FMT 8.23 12.86 0 2.52 8 DOLLARS AND 23 CENTS 12 DOLLARS AND 86 CENTS 2 DOLLARS AND 52 CENTS
⎕FMT is a very powerful function which allows you to format numbers in all sorts of ways. Not surprisingly it is one of the most complex functions available in modern APLs.
End of Tutorial
That's the end of the APL Tutorial. Congratulations on making it all the way to the end! If you're still hungry for more, you might want to check out Further Topics in APL which covers some more advanced topics.