|
4 | 4 |
|
5 | 5 |
|
6 | 6 |
|
7 |
| - <suite name="General Operations" code="****"> |
8 |
| - |
9 |
| - |
| 7 | + <suite name="Number Conversion" code="****"> |
10 | 8 |
|
11 | 9 | <command name="parse number" code="Mth:PNum" description="parse text as a number">
|
12 | 10 | <direct-parameter type="text"/>
|
13 |
| - <parameter name="using" code="Usin" type="FNSt" optional="yes" description="(default: default format)"/> |
| 11 | + <parameter name="using" code="Usin" optional="yes" description="(default: default format)"> |
| 12 | + <type type="MthZ"/> |
| 13 | + <type type="number format record"/> |
| 14 | + </parameter> |
14 | 15 | <parameter name="for locale" code="Loca" optional="yes" description="a locale identifier, e.g. “en_US” (default: no locale)">
|
15 | 16 | <type type="text"/>
|
16 | 17 | <type type="LclE"/>
|
17 | 18 | </parameter>
|
18 | 19 | <result type="number"/>
|
19 | 20 | <documentation>
|
20 | 21 | <html><![CDATA[
|
21 |
| - |
22 |
| -<p>By default, the <code>parse number</code> command converts numeric text in canonical format to an integer/real number. Unlike coercing a text value to <code>number</code>, which parses the text according to the current user’s localization settings, this always uses the same numerical syntax as AppleScript itself. For example:</p> |
| 22 | +<h3>Basic Use</h3> |
| 23 | +
|
| 24 | +<p>By default, the <code>parse number</code> command converts numeric text in canonical format to an integer/real number. Unlike coercing a text value to <code>number</code>, which parses the text according to the current user’s localization settings, <code>format number</code> <em>always</em> uses the same numerical syntax as the AppleScript language, unless additional formatting and/or locale settings are explicitly given.</p> |
| 25 | +
|
| 26 | +<p>For example, on a US-localized system, coercing <code>"3.14"</code> to a number produces <code>3.14</code>:</p> |
| 27 | +
|
| 28 | +<pre><code>"3.14" as number → 3.14 -- localized conversion |
| 29 | +
|
| 30 | +parse number "3.14" → 3.14 -- canonical conversion</code></pre> |
23 | 31 |
|
24 |
| -<pre><code>"3.14" as number → 3.14 (USA) |
25 |
| -"3,14" as number → 3.14 (Germany) |
| 32 | +<p>On a German system, however, the same text-to-number coercion requires the decimal separator to be a comma, not a period:</p> |
26 | 33 |
|
27 |
| -convert text to number "3.14" → 3.14 (USA) |
28 |
| -convert text to number "3.14" → 3.14 (Germany)</code></pre> |
| 34 | +<pre><code>"3.14" as number → Error: Can’t make "3.14" into type number. |
| 35 | +
|
| 36 | +<strong>"3,14"</strong> as number → 3.14 -- localized conversion |
| 37 | +
|
| 38 | +parse number "3.14" → 3.14 -- canonical conversion</code></pre> |
| 39 | +
|
| 40 | +<p>Using the <code>parse number</code> command instead of coercing the number to text ensures a consistent result, no matter where the script is run.</p> |
| 41 | +
|
| 42 | +<!-- TO DO: custom formatting discussion and examples --> |
29 | 43 |
|
30 | 44 | ]]></html>
|
31 | 45 | </documentation>
|
32 | 46 | </command>
|
33 | 47 |
|
34 | 48 | <command name="format number" code="Mth:FNum" description="format a number as text">
|
35 | 49 | <direct-parameter type="number"/>
|
36 |
| - <parameter name="using" code="Usin" type="FNSt" optional="yes" description="(default: default format)"/> |
| 50 | + <parameter name="using" code="Usin" optional="yes" description="(default: default format)"> |
| 51 | + <type type="MthZ"/> |
| 52 | + <type type="text"/> |
| 53 | + <type type="number format record"/> |
| 54 | + </parameter> |
37 | 55 | <parameter name="for locale" code="Loca" optional="yes" description="a locale identifier, e.g. “en_US” (default: no locale)">
|
38 | 56 | <type type="text"/>
|
39 | 57 | <type type="LclE"/>
|
40 | 58 | </parameter>
|
41 | 59 | <result type="text"/>
|
42 | 60 | <documentation>
|
43 | 61 | <html><![CDATA[
|
| 62 | +<h3>Basic Use</h3> |
44 | 63 |
|
45 |
| -<p>By default, the <code>format number</code> command converts an integer/real number to numeric text in canonical format. Unlike coercing an integer or real value to <code>text</code>, which formats the text according to the current user’s localization settings, <code>format number</code> always uses the same numerical syntax as AppleScript itself, unless a locale is explicitly specified.</p> |
| 64 | +<p>By default, the <code>format number</code> command converts an integer/real number to numeric text in canonical format. Unlike coercing an integer or real value to <code>text</code>, which formats the text according to the current user’s localization settings, <code>format number</code> <em>always</em> uses the same numerical syntax as the AppleScript language, unless additional formatting and/or locale settings are explicitly given.</p> |
46 | 65 |
|
47 | 66 | <p>For example, on a US-localized system, coercing <code>3.14</code> to text produces <code>"3.14"</code>:</p>
|
48 | 67 |
|
49 |
| -<pre><code>3.14 as text → "3.14" |
| 68 | +<pre><code>3.14 as text → "3.14" -- localized conversion |
| 69 | +
|
| 70 | +format number 3.14 → "3.14" -- canonical conversion</code></pre> |
| 71 | +
|
| 72 | +<p>On a German system, however, the same number-to-text coercion uses a comma instead of a period as the decimal separator:</p> |
50 | 73 |
|
51 |
| -format number 3.14 → "3.14"</code></pre> |
| 74 | +<pre><code>3.14 as text → <strong>"3,14"</strong> -- localized conversion |
52 | 75 |
|
53 |
| -whereas on a German system, the same coercion produces <code>"3,14"</code> instead, using a comma instead of a period as the decimal separator: |
| 76 | +format number 3.14 → "3.14" -- canonical conversion</code></pre> |
54 | 77 |
|
55 |
| -<pre><code>3.14 as text → <strong>"3,14"</strong> |
| 78 | +<p>Using the <code>format number</code> command instead of coercing the number to text ensures a consistent result, no matter where the script is run.</p> |
56 | 79 |
|
57 |
| -format number 3.14 → "3.14"</code></pre> |
| 80 | +<!-- TO DO: custom formatting discussion and examples --> |
58 | 81 |
|
59 |
| -<p>By disregarding the current locale, the <code>format number</code> command ensures the same text value will be produced no matter where the script is run.</p> |
60 | 82 | ]]></html>
|
61 | 83 | </documentation>
|
62 | 84 | </command>
|
63 | 85 |
|
64 |
| - <enumeration name="FNSt" code="FNSt"> |
65 |
| - <enumerator name="default format" code="FNSD"/> |
66 |
| - <enumerator name="integer format" code="FNS0"/> |
67 |
| - <enumerator name="decimal format" code="FNS1"/> |
68 |
| - <enumerator name="currency format" code="FNS2"/> |
69 |
| - <enumerator name="percent format" code="FNS3"/> |
70 |
| - <enumerator name="scientific format" code="FNS4"/> |
71 |
| - <enumerator name="word format" code="FNS5"/> |
| 86 | + |
| 87 | + <record-type name="number format definition" code="MthR"> |
| 88 | + <property name="basic format" code="MthA"> |
| 89 | + <type type="MthZ"/> |
| 90 | + <type type="text"/> |
| 91 | + </property> |
| 92 | + <property name="minimum decimal places" code="MthB" type="integer"/> |
| 93 | + <property name="maximum decimal places" code="MthC" type="integer"/> |
| 94 | + <property name="minimum significant digits" code="MthD" type="integer"/> |
| 95 | + <property name="maximum significant digits" code="MthE" type="integer"/> |
| 96 | + <property name="decimal separator" code="MthF" type="text"/> |
| 97 | + <property name="grouping separator" code="MthG" type="text"/> |
| 98 | + <property name="rounding behavior" code="MthH" type="MRnd"/> |
| 99 | + </record-type> |
| 100 | + |
| 101 | + |
| 102 | + <enumeration name="MthZ" code="MthZ"> |
| 103 | + <enumerator name="default format" code="Mth0"/> |
| 104 | + <enumerator name="integer format" code="Mth1"/> |
| 105 | + <enumerator name="decimal format" code="Mth2"/> |
| 106 | + <enumerator name="scientific format" code="Mth3"/> |
| 107 | + <enumerator name="percent format" code="Mth4"/> |
| 108 | + <enumerator name="currency format" code="Mth5"/> |
| 109 | + <enumerator name="word format" code="Mth6"/> |
72 | 110 | </enumeration>
|
73 | 111 |
|
74 | 112 | <enumeration name="LclE" code="LclE">
|
@@ -97,9 +135,12 @@ format number 3.14 → "3.14"</code></pre>
|
97 | 135 | <type type="integer" list="yes"/>
|
98 | 136 | </result>
|
99 | 137 | </command>
|
100 |
| - |
101 |
| - |
102 |
| - |
| 138 | + |
| 139 | + </suite> |
| 140 | + |
| 141 | + |
| 142 | + |
| 143 | + <suite name="General Operations" code="****"> |
103 | 144 |
|
104 | 145 | <command name="deg2rad" code="Mth:DeRa" description="convert an angle from degrees to radians">
|
105 | 146 | <direct-parameter type="real"/>
|
@@ -161,14 +202,10 @@ cmp {(0.7 * 0.7), 0.49} → 0 (i.e. the numbers are "equal")</code></pre>
|
161 | 202 |
|
162 | 203 |
|
163 | 204 | <command name="round number" code="Mth:RouN" description="round a number to the specified number of places">
|
164 |
| - <direct-parameter type="real"/> |
| 205 | + <direct-parameter type="number"/> |
165 | 206 | <parameter name="to places" code="Plac" type="integer" optional="yes" description="The maximum number of decimal places that can appear in the result (default: 0). If 0, the result is an integer; if greater than 0, the number's least significant digits are also rounded."/>
|
166 |
| - <parameter name="by" code="Dire" type="MRnd" optional="yes" description="the rounding direction (default: ‘rounding halves to even’)"/> |
167 |
| - <result> |
168 |
| - <type type="real"/> |
169 |
| - <type type="integer"/> |
170 |
| - </result> |
171 |
| - <!-- TO DO: need documentation to fully explain each rounding behavior --> |
| 207 | + <parameter name="by" code="Dire" type="MRnd" optional="yes" description="the rounding direction (default: rounding halves to even)"/> |
| 208 | + <result type="number"/> |
172 | 209 | </command>
|
173 | 210 |
|
174 | 211 |
|
|
0 commit comments