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/nums.md | |
| 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/nums.md')
| -rw-r--r-- | api/nums.md | 78 |
1 files changed, 32 insertions, 46 deletions
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 |
