File tree Expand file tree Collapse file tree 2 files changed +23
-3
lines changed Expand file tree Collapse file tree 2 files changed +23
-3
lines changed Original file line number Diff line number Diff line change @@ -78,9 +78,8 @@ defmodule Algora.Util do
78
78
def format_pct ( percentage ) do
79
79
percentage
80
80
|> Decimal . mult ( 100 )
81
- |> Decimal . to_string ( )
82
- |> String . trim_trailing ( "0" )
83
- |> String . trim_trailing ( "." )
81
+ |> Decimal . normalize ( )
82
+ |> Decimal . to_string ( :normal )
84
83
|> Kernel . <> ( "%" )
85
84
end
86
85
Original file line number Diff line number Diff line change
1
+ defmodule Algora.UtilTest do
2
+ use ExUnit.Case , async: true
3
+
4
+ alias Algora.Util
5
+
6
+ describe "format_pct/1" do
7
+ test "formats decimal percentages correctly" do
8
+ assert Util . format_pct ( Decimal . new ( "1" ) ) == "100%"
9
+ assert Util . format_pct ( Decimal . new ( "0.1" ) ) == "10%"
10
+ assert Util . format_pct ( Decimal . new ( "0.156" ) ) == "15.6%"
11
+ assert Util . format_pct ( Decimal . new ( "0.1567" ) ) == "15.67%"
12
+ assert Util . format_pct ( Decimal . new ( "0.15678" ) ) == "15.678%"
13
+ assert Util . format_pct ( Decimal . new ( "0" ) ) == "0%"
14
+ end
15
+
16
+ test "trims trailing zeros" do
17
+ assert Util . format_pct ( Decimal . new ( "0.1500" ) ) == "15%"
18
+ assert Util . format_pct ( Decimal . new ( "0.1050" ) ) == "10.5%"
19
+ end
20
+ end
21
+ end
You can’t perform that action at this time.
0 commit comments