diff options
author | versecafe <147033096+versecafe@users.noreply.github.com> | 2024-09-03 08:54:02 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-09-03 14:54:02 +0100 |
commit | ee334747949c6696a0128d1ce709078ecc564598 (patch) | |
tree | 6099b3f73f4d0b371ee2046ee3288d3400cd54a7 | |
parent | a349b6273717d42c232079b0bfcf5002d10e10d3 (diff) | |
download | gleam_stdlib-ee334747949c6696a0128d1ce709078ecc564598.tar.gz gleam_stdlib-ee334747949c6696a0128d1ce709078ecc564598.zip |
Add `to_precision` to `gleam/float` (#668)
-rw-r--r-- | CHANGELOG.md | 1 | ||||
-rw-r--r-- | src/gleam/float.gleam | 26 | ||||
-rw-r--r-- | src/gleam_stdlib.mjs | 44 | ||||
-rw-r--r-- | test/gleam/float_test.gleam | 26 |
4 files changed, 82 insertions, 15 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index ea984be..0754990 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## v0.41.0 - Unreleased - The `bit_array` module gains the `compare` function. +- The `float` modeule gains the `to_precision` function. ## v0.40.0 - 2024-08-19 diff --git a/src/gleam/float.gleam b/src/gleam/float.gleam index 597f4ef..7030caa 100644 --- a/src/gleam/float.gleam +++ b/src/gleam/float.gleam @@ -267,6 +267,32 @@ pub fn truncate(x: Float) -> Int { @external(javascript, "../gleam_stdlib.mjs", "truncate") fn do_truncate(a: Float) -> Int +/// Converts the value to a given precision as a `Float`. +/// The precision is the number of allowed decimal places. +/// Negative precisions are allowed and force rounding +/// to the nearest tenth, hundredth, thousandth etc. +/// +/// ## Examples +/// +/// ```gleam +/// to_precision(2.43434348473, precision: 2) +/// // -> 2.43 +/// ``` +/// +/// ```gleam +/// to_precision(547890.453444, precision: -3) +/// // -> 548000.0 +/// ``` +/// +pub fn to_precision(x: Float, precision: Int) -> Float { + let factor = do_power(10.0, do_to_float(-precision)) + do_to_float(round(x /. factor)) *. factor +} + +@external(erlang, "erlang", "float") +@external(javascript, "../gleam_stdlib.mjs", "identity") +fn do_to_float(a: Int) -> Float + /// Returns the absolute value of the input as a `Float`. /// /// ## Examples diff --git a/src/gleam_stdlib.mjs b/src/gleam_stdlib.mjs index 9fe6caa..50ebb46 100644 --- a/src/gleam_stdlib.mjs +++ b/src/gleam_stdlib.mjs @@ -426,7 +426,7 @@ export function compile_regex(pattern, options) { export function regex_split(regex, string) { return List.fromArray( string.split(regex).map((item) => (item === undefined ? "" : item)), - ); + ); } export function regex_scan(regex, string) { @@ -448,7 +448,7 @@ export function regex_scan(regex, string) { } export function regex_replace(regex, original_string, replacement) { - return original_string.replaceAll(regex, replacement) + return original_string.replaceAll(regex, replacement); } export function new_map() { @@ -550,8 +550,7 @@ export function encode64(bit_array, padding) { if (padding) { if (k === 1) { base64 += "=="; - } - else if (k === 2) { + } else if (k === 2) { base64 += "="; } } @@ -566,7 +565,7 @@ export function decode64(sBase64) { const length = binString.length; const array = new Uint8Array(length); for (let i = 0; i < length; i++) { - array[i] = binString.charCodeAt(i); + array[i] = binString.charCodeAt(i); } return new Ok(new BitArray(array)); } catch { @@ -827,25 +826,40 @@ export function inspect(v) { } function inspectString(str) { - let new_str = "\""; + let new_str = '"'; for (let i = 0; i < str.length; i++) { let char = str[i]; switch (char) { - case '\n': new_str += "\\n"; break; - case '\r': new_str += "\\r"; break; - case '\t': new_str += "\\t"; break; - case '\f': new_str += "\\f"; break; - case '\\': new_str += "\\\\"; break; - case '\"': new_str += "\\\""; break; + case "\n": + new_str += "\\n"; + break; + case "\r": + new_str += "\\r"; + break; + case "\t": + new_str += "\\t"; + break; + case "\f": + new_str += "\\f"; + break; + case "\\": + new_str += "\\\\"; + break; + case '"': + new_str += '\\"'; + break; default: - if (char < ' ' || (char > '~' && char < '\u{00A0}')) { - new_str += "\\u{" + char.charCodeAt(0).toString(16).toUpperCase().padStart(4, "0") + "}"; + if (char < " " || (char > "~" && char < "\u{00A0}")) { + new_str += + "\\u{" + + char.charCodeAt(0).toString(16).toUpperCase().padStart(4, "0") + + "}"; } else { new_str += char; } } } - new_str += "\""; + new_str += '"'; return new_str; } diff --git a/test/gleam/float_test.gleam b/test/gleam/float_test.gleam index 4c49f69..6a737e7 100644 --- a/test/gleam/float_test.gleam +++ b/test/gleam/float_test.gleam @@ -213,6 +213,32 @@ pub fn truncate_test() { |> should.equal(-7) } +pub fn to_precision_test() { + float.to_precision(2.43434348473, 2) + |> should.equal(2.43) + + float.to_precision(2.43534348473, 2) + |> should.equal(2.44) + + float.to_precision(-2.43534348473, 2) + |> should.equal(-2.44) + + float.to_precision(547_890.453444, -3) + |> should.equal(548_000.0) + + float.to_precision(547_490.453444, -3) + |> should.equal(547_000.0) + + float.to_precision(-547_490.453444, -3) + |> should.equal(-547_000.0) + + float.to_precision(435.3224, 0) + |> should.equal(435.0) + + float.to_precision(435.3224, -0) + |> should.equal(435.0) +} + pub fn min_test() { float.min(0.0, 0.0) |> should.equal(0.0) |