diff options
| -rw-r--r-- | docs/nums.md | 26 | ||||
| -rw-r--r-- | src/environment.c | 2 | ||||
| -rw-r--r-- | src/stdlib/nums.c | 8 | ||||
| -rw-r--r-- | src/stdlib/nums.h | 2 | ||||
| -rw-r--r-- | test/nums.tm | 3 |
5 files changed, 40 insertions, 1 deletions
diff --git a/docs/nums.md b/docs/nums.md index 7a86a585..02859d28 100644 --- a/docs/nums.md +++ b/docs/nums.md @@ -129,7 +129,7 @@ called either on the type itself: `Num.sqrt(x)` or as a method call: - [`func atanh(x: Num -> Num)`](#atanh) - [`func cbrt(x: Num -> Num)`](#cbrt) - [`func ceil(x: Num -> Num)`](#ceil) -- [`clamped(x, low, high: Num -> Num)`](#clamped) +- [`func clamped(x, low, high: Num -> Num)`](#clamped) - [`func copysign(x: Num, y: Num -> Num)`](#copysign) - [`func cos(x: Num -> Num)`](#cos) - [`func cosh(x: Num -> Num)`](#cosh) @@ -155,6 +155,7 @@ called either on the type itself: `Num.sqrt(x)` or as a method call: - [`func near(x: Num, y: Num, ratio: Num = 1e-9, min_epsilon: Num = 1e-9 -> Bool)`](#near) - [`func nextafter(x: Num, y: Num -> Num)`](#nextafter) - [`func parse(text: Text -> Num?)`](#parse) +- [`func percent(n: Num -> Text)`](#percent) - [`func rint(x: Num -> Num)`](#rint) - [`func round(x: Num -> Num)`](#round) - [`func scientific(n: Num, precision: Int = 0 -> Text)`](#scientific) @@ -922,6 +923,29 @@ as a number. --- +### `percent` +Convert a number into a percentage text with a percent sign. + +```tomo +func percent(n: Num, precision: Int = 0 -> Text) +``` + +- `n`: The number to be converted to a percent. +- `precision`: The number of decimal places. Default is `0`. + +**Returns:** +A text representation of the number as a percentage with a percent sign. + +**Example:** +```tomo +>> 0.5:percent() += "50%" +>> (1./3.):percent(2) += "33.33%" +``` + +--- + ### `rint` Rounds a number to the nearest integer, with ties rounded to the nearest even integer. diff --git a/src/environment.c b/src/environment.c index 85677524..94461305 100644 --- a/src/environment.c +++ b/src/environment.c @@ -223,6 +223,7 @@ env_t *global_env(void) {"clamped", "Num$clamped", "func(x,low,high:Num -> Num)"}, {"format", "Num$format", "func(n:Num, precision=16 -> Text)"}, {"scientific", "Num$scientific", "func(n:Num,precision=0 -> Text)"}, + {"percent", "Num$percent", "func(n:Num,precision=0 -> Text)"}, {"isinf", "Num$isinf", "func(n:Num -> Bool)"}, {"isfinite", "Num$isfinite", "func(n:Num -> Bool)"}, {"modulo", "Num$mod", "func(x,y:Num -> Num)"}, @@ -254,6 +255,7 @@ env_t *global_env(void) {"clamped", "Num32$clamped", "func(x,low,high:Num32 -> Num32)"}, {"format", "Num32$format", "func(n:Num32, precision=8 -> Text)"}, {"scientific", "Num32$scientific", "func(n:Num32, precision=0 -> Text)"}, + {"percent", "Num32$percent", "func(n:Num32,precision=0 -> Text)"}, {"isinf", "Num32$isinf", "func(n:Num32 -> Bool)"}, {"isfinite", "Num32$isfinite", "func(n:Num32 -> Bool)"}, C(2_SQRTPI), C(E), C(PI_2), C(2_PI), C(1_PI), C(LN10), C(LN2), C(LOG2E), diff --git a/src/stdlib/nums.c b/src/stdlib/nums.c index 98f7b509..93e2b21b 100644 --- a/src/stdlib/nums.c +++ b/src/stdlib/nums.c @@ -57,6 +57,10 @@ public Text_t Num$scientific(double f, Int_t precision) { return Text$format("%.*e", (int)Int64$from_int(precision, false), f); } +public Text_t Num$percent(double f, Int_t precision) { + return Text$format("%.*f%%", (int)Int64$from_int(precision, false), 100.*f); +} + public CONSTFUNC double Num$mod(double num, double modulus) { // Euclidean division, see: https://www.microsoft.com/en-us/research/wp-content/uploads/2016/02/divmodnote-letter.pdf double r = remainder(num, modulus); @@ -138,6 +142,10 @@ public Text_t Num32$scientific(float f, Int_t precision) { return Text$format("%.*e", (int)Int64$from_int(precision, false), (double)f); } +public Text_t Num32$percent(float f, Int_t precision) { + return Text$format("%.*f%%", (int)Int64$from_int(precision, false), 100.*(double)f); +} + public CONSTFUNC float Num32$mod(float num, float modulus) { // Euclidean division, see: https://www.microsoft.com/en-us/research/wp-content/uploads/2016/02/divmodnote-letter.pdf float r = remainderf(num, modulus); diff --git a/src/stdlib/nums.h b/src/stdlib/nums.h index af0e895b..be270f51 100644 --- a/src/stdlib/nums.h +++ b/src/stdlib/nums.h @@ -23,6 +23,7 @@ PUREFUNC bool Num$equal(const void *x, const void *y, const TypeInfo_t *type); CONSTFUNC bool Num$near(double a, double b, double ratio, double absolute); Text_t Num$format(double f, Int_t precision); Text_t Num$scientific(double f, Int_t precision); +Text_t Num$percent(double f, Int_t precision); double Num$mod(double num, double modulus); double Num$mod1(double num, double modulus); CONSTFUNC bool Num$isinf(double n); @@ -74,6 +75,7 @@ PUREFUNC bool Num32$equal(const void *x, const void *y, const TypeInfo_t *type); CONSTFUNC bool Num32$near(float a, float b, float ratio, float absolute); Text_t Num32$format(float f, Int_t precision); Text_t Num32$scientific(float f, Int_t precision); +Text_t Num32$percent(float f, Int_t precision); float Num32$mod(float num, float modulus); float Num32$mod1(float num, float modulus); CONSTFUNC bool Num32$isinf(float n); diff --git a/test/nums.tm b/test/nums.tm index 1192826f..e5fd27e2 100644 --- a/test/nums.tm +++ b/test/nums.tm @@ -69,3 +69,6 @@ func main(): >> Num(5) = 5 : Num + + >> 0.5:percent() + = "50%" |
