diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2025-04-27 16:49:38 -0400 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2025-04-27 16:49:38 -0400 |
| commit | baf990e65c62f42e45fe25ac385db9536d3f1788 (patch) | |
| tree | 7b89cecde1d6cd3b600b9295e0ad22a702cd1065 /api | |
| parent | 1a8a8bc3e2ebd9c282db8131902f9bb5de8c03cb (diff) | |
Update stdlib to use `print` instead of `printf` in all cases. This
means bringing in fpconv to do float-to-string conversion and a few
updates to integer and number methods for string formatting.
Diffstat (limited to 'api')
| -rw-r--r-- | api/api.md | 100 | ||||
| -rw-r--r-- | api/integers.md | 22 | ||||
| -rw-r--r-- | api/integers.yaml | 22 | ||||
| -rw-r--r-- | api/nums.md | 78 | ||||
| -rw-r--r-- | api/nums.yaml | 79 |
5 files changed, 96 insertions, 205 deletions
@@ -402,28 +402,6 @@ n | `Int` | The integer to compute the factorial of. | - = 3628800 ``` -## Int.format - -```tomo -Int.format : func(i: Int, digits: Int = 0 -> Text) -``` - -Formats an integer as a string with a specified number of digits. - -Argument | Type | Description | Default ----------|------|-------------|--------- -i | `Int` | The integer to be formatted. | - -digits | `Int` | The minimum number of digits to which the integer should be padded. | `0` - -**Return:** A string representation of the integer, padded to the specified number of digits. - - -**Example:** -```tomo ->> (42).format(digits=5) -= "00042" - -``` ## Int.hex ```tomo @@ -1890,28 +1868,6 @@ x | `Num` | The number to be rounded down. | - = 3 ``` -## Num.format - -```tomo -Num.format : func(n: Num, precision: Int = 0 -> Text) -``` - -Formats a number as a text with a specified precision. - -Argument | Type | Description | Default ----------|------|-------------|--------- -n | `Num` | The number to be formatted. | - -precision | `Int` | The number of decimal places. Default is `0`. | `0` - -**Return:** A text representation of the number with the specified precision. - - -**Example:** -```tomo ->> (3.14159).format(precision=2) -= "3.14" - -``` ## Num.hypot ```tomo @@ -2257,7 +2213,7 @@ text | `Text` | The text containing the number. | - ## Num.percent ```tomo -Num.percent : func(n: Num, precision: Int = 0 -> Text) +Num.percent : func(n: Num, precision: Num = 0.01 -> Text) ``` Convert a number into a percentage text with a percent sign. @@ -2265,7 +2221,7 @@ Convert a number into a percentage text with a percent sign. Argument | Type | Description | Default ---------|------|-------------|--------- n | `Num` | The number to be converted to a percent. | - -precision | `Int` | The number of decimal places. Default is `0`. | `0` +precision | `Num` | Round the percentage to this precision level. | `0.01` **Return:** A text representation of the number as a percentage with a percent sign. @@ -2276,6 +2232,10 @@ precision | `Int` | The number of decimal places. Default is `0`. | `0` = "50%" >> (1./3.).percent(2) = "33.33%" +>> (1./3.).percent(2, precision=0.0001) += "33.3333%" +>> (1./3.).percent(2, precision=10.) += "30%" ``` ## Num.rint @@ -2324,28 +2284,6 @@ x | `Num` | The number to be rounded. | - = 3 ``` -## Num.scientific - -```tomo -Num.scientific : func(n: Num, precision: Int = 0 -> Text) -``` - -Formats a number in scientific notation with a specified precision. - -Argument | Type | Description | Default ----------|------|-------------|--------- -n | `Num` | The number to be formatted. | - -precision | `Int` | The number of decimal places. Default is `0`. | `0` - -**Return:** A text representation of the number in scientific notation with the specified precision. - - -**Example:** -```tomo ->> (12345.6789).scientific(precision=2) -= "1.23e+04" - -``` ## Num.significand ```tomo @@ -2516,6 +2454,32 @@ x | `Num` | The number to be truncated. | - = -3 ``` +## Num.with_precision + +```tomo +Num.with_precision : func(n: Num, precision: Num -> Num) +``` + +Round a number to the given precision level (specified as `10`, `.1`, `.001` etc). + +Argument | Type | Description | Default +---------|------|-------------|--------- +n | `Num` | The number to be rounded to a given precision. | - +precision | `Num` | The precision to which the number should be rounded. | - + +**Return:** The number, rounded to the given precision level. + + +**Example:** +```tomo +>> (0.1234567).with_precision(0.01) += 0.12 +>> (123456.).with_precision(100) += 123500 +>> (1234567.).with_precision(5) += 1234565 + +``` ## Num.y0 ```tomo diff --git a/api/integers.md b/api/integers.md index d79812df..0865e93f 100644 --- a/api/integers.md +++ b/api/integers.md @@ -90,28 +90,6 @@ n | `Int` | The integer to compute the factorial of. | - = 3628800 ``` -## Int.format - -```tomo -Int.format : func(i: Int, digits: Int = 0 -> Text) -``` - -Formats an integer as a string with a specified number of digits. - -Argument | Type | Description | Default ----------|------|-------------|--------- -i | `Int` | The integer to be formatted. | - -digits | `Int` | The minimum number of digits to which the integer should be padded. | `0` - -**Return:** A string representation of the integer, padded to the specified number of digits. - - -**Example:** -```tomo ->> (42).format(digits=5) -= "00042" - -``` ## Int.hex ```tomo diff --git a/api/integers.yaml b/api/integers.yaml index 4e5ec46c..f927c75f 100644 --- a/api/integers.yaml +++ b/api/integers.yaml @@ -82,28 +82,6 @@ Int.factorial: >> (10).factorial() = 3628800 -Int.format: - short: text formatting - description: > - Formats an integer as a string with a specified number of digits. - return: - type: 'Text' - description: > - A string representation of the integer, padded to the specified number of digits. - args: - i: - type: 'Int' - description: > - The integer to be formatted. - digits: - type: 'Int' - default: '0' - description: > - The minimum number of digits to which the integer should be padded. - example: | - >> (42).format(digits=5) - = "00042" - Int.hex: short: convert to hexidecimal description: > diff --git a/api/nums.md b/api/nums.md index e42329f9..471a7739 100644 --- a/api/nums.md +++ b/api/nums.md @@ -563,28 +563,6 @@ x | `Num` | The number to be rounded down. | - = 3 ``` -## Num.format - -```tomo -Num.format : func(n: Num, precision: Int = 0 -> Text) -``` - -Formats a number as a text with a specified precision. - -Argument | Type | Description | Default ----------|------|-------------|--------- -n | `Num` | The number to be formatted. | - -precision | `Int` | The number of decimal places. Default is `0`. | `0` - -**Return:** A text representation of the number with the specified precision. - - -**Example:** -```tomo ->> (3.14159).format(precision=2) -= "3.14" - -``` ## Num.hypot ```tomo @@ -930,7 +908,7 @@ text | `Text` | The text containing the number. | - ## Num.percent ```tomo -Num.percent : func(n: Num, precision: Int = 0 -> Text) +Num.percent : func(n: Num, precision: Num = 0.01 -> Text) ``` Convert a number into a percentage text with a percent sign. @@ -938,7 +916,7 @@ Convert a number into a percentage text with a percent sign. Argument | Type | Description | Default ---------|------|-------------|--------- n | `Num` | The number to be converted to a percent. | - -precision | `Int` | The number of decimal places. Default is `0`. | `0` +precision | `Num` | Round the percentage to this precision level. | `0.01` **Return:** A text representation of the number as a percentage with a percent sign. @@ -949,6 +927,10 @@ precision | `Int` | The number of decimal places. Default is `0`. | `0` = "50%" >> (1./3.).percent(2) = "33.33%" +>> (1./3.).percent(2, precision=0.0001) += "33.3333%" +>> (1./3.).percent(2, precision=10.) += "30%" ``` ## Num.rint @@ -997,28 +979,6 @@ x | `Num` | The number to be rounded. | - = 3 ``` -## Num.scientific - -```tomo -Num.scientific : func(n: Num, precision: Int = 0 -> Text) -``` - -Formats a number in scientific notation with a specified precision. - -Argument | Type | Description | Default ----------|------|-------------|--------- -n | `Num` | The number to be formatted. | - -precision | `Int` | The number of decimal places. Default is `0`. | `0` - -**Return:** A text representation of the number in scientific notation with the specified precision. - - -**Example:** -```tomo ->> (12345.6789).scientific(precision=2) -= "1.23e+04" - -``` ## Num.significand ```tomo @@ -1189,6 +1149,32 @@ x | `Num` | The number to be truncated. | - = -3 ``` +## Num.with_precision + +```tomo +Num.with_precision : func(n: Num, precision: Num -> Num) +``` + +Round a number to the given precision level (specified as `10`, `.1`, `.001` etc). + +Argument | Type | Description | Default +---------|------|-------------|--------- +n | `Num` | The number to be rounded to a given precision. | - +precision | `Num` | The precision to which the number should be rounded. | - + +**Return:** The number, rounded to the given precision level. + + +**Example:** +```tomo +>> (0.1234567).with_precision(0.01) += 0.12 +>> (123456.).with_precision(100) += 123500 +>> (1234567.).with_precision(5) += 1234565 + +``` ## Num.y0 ```tomo diff --git a/api/nums.yaml b/api/nums.yaml index 37b2c7c5..e666a0d6 100644 --- a/api/nums.yaml +++ b/api/nums.yaml @@ -378,28 +378,6 @@ Num.floor: >> (3.7).floor() = 3 -Num.format: - short: convert a number to text - description: > - Formats a number as a text with a specified precision. - return: - type: 'Text' - description: > - A text representation of the number with the specified precision. - args: - n: - type: 'Num' - description: > - The number to be formatted. - precision: - type: 'Int' - default: '0' - description: > - The number of decimal places. Default is `0`. - example: | - >> (3.14159).format(precision=2) - = "3.14" - Num.hypot: short: Euclidean distance function description: > @@ -728,15 +706,44 @@ Num.percent: description: > The number to be converted to a percent. precision: - type: 'Int' - default: '0' + type: 'Num' + default: '0.01' description: > - The number of decimal places. Default is `0`. + Round the percentage to this precision level. example: | >> (0.5).percent() = "50%" >> (1./3.).percent(2) = "33.33%" + >> (1./3.).percent(2, precision=0.0001) + = "33.3333%" + >> (1./3.).percent(2, precision=10.) + = "30%" + +Num.with_precision: + short: round to a given precision + description: > + Round a number to the given precision level (specified as `10`, `.1`, `.001` etc). + return: + type: 'Num' + description: > + The number, rounded to the given precision level. + args: + n: + type: 'Num' + description: > + The number to be rounded to a given precision. + precision: + type: 'Num' + description: > + The precision to which the number should be rounded. + example: | + >> (0.1234567).with_precision(0.01) + = 0.12 + >> (123456.).with_precision(100) + = 123500 + >> (1234567.).with_precision(5) + = 1234565 Num.rint: short: round to nearest integer @@ -776,28 +783,6 @@ Num.round: >> (2.7).round() = 3 -Num.scientific: - short: format in scientific notation - description: > - Formats a number in scientific notation with a specified precision. - return: - type: 'Text' - description: > - A text representation of the number in scientific notation with the specified precision. - args: - n: - type: 'Num' - description: > - The number to be formatted. - precision: - type: 'Int' - default: '0' - description: > - The number of decimal places. Default is `0`. - example: | - >> (12345.6789).scientific(precision=2) - = "1.23e+04" - Num.significand: short: get mantissa description: > |
