diff options
author | yoshi~ <jreusch4@gmail.com> | 2025-01-02 17:21:20 +0100 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2025-01-03 20:44:56 +0000 |
commit | 619935a6e79dce3262712838c4cb1c1b69f33132 (patch) | |
tree | bab9c83c3dea2c09f7f0fff5bb6c314240438390 /src | |
parent | f9aea4a694fff1bd54cc43c807635a579cf8af8d (diff) | |
download | gleam_stdlib-619935a6e79dce3262712838c4cb1c1b69f33132.tar.gz gleam_stdlib-619935a6e79dce3262712838c4cb1c1b69f33132.zip |
improve precision
Diffstat (limited to 'src')
-rw-r--r-- | src/gleam/float.gleam | 12 |
1 files changed, 10 insertions, 2 deletions
diff --git a/src/gleam/float.gleam b/src/gleam/float.gleam index 34dba05..6aeeac7 100644 --- a/src/gleam/float.gleam +++ b/src/gleam/float.gleam @@ -261,8 +261,16 @@ pub fn truncate(x: Float) -> Int /// ``` /// 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 + case precision <= 0 { + True -> { + let factor = do_power(10.0, do_to_float(-precision)) + do_to_float(round(x /. factor)) *. factor + } + False -> { + let factor = do_power(10.0, do_to_float(precision)) + do_to_float(round(x *. factor)) /. factor + } + } } @external(erlang, "erlang", "float") |