This repository has been archived by the owner on Feb 28, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #71 from datasciencebr/cuducos-decimal-ui
Format prices in the UI
- Loading branch information
Showing
4 changed files
with
154 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
module Format.Number exposing (formatNumber) | ||
|
||
import String | ||
|
||
|
||
addThousandSeparator : String -> String -> String | ||
addThousandSeparator separator num = | ||
let | ||
parts : List String | ||
parts = | ||
String.split separator num | ||
|
||
firstPart : String | ||
firstPart = | ||
List.head parts |> Maybe.withDefault "" | ||
|
||
remainingParts : List String | ||
remainingParts = | ||
List.tail parts |> Maybe.withDefault [] | ||
|
||
firstParts : List String | ||
firstParts = | ||
if String.length firstPart > 3 then | ||
let | ||
newFirstPart : String | ||
newFirstPart = | ||
String.dropRight 3 firstPart | ||
in | ||
[ addThousandSeparator separator newFirstPart | ||
, String.right 3 firstPart | ||
] | ||
else | ||
[ firstPart ] | ||
in | ||
String.join separator <| | ||
List.concat | ||
[ firstParts | ||
, remainingParts | ||
] | ||
|
||
|
||
formatNumber : Int -> String -> String -> Float -> String | ||
formatNumber decimals thousandSeparetor decimalSeparator num = | ||
if num == 0 then | ||
"0" ++ decimalSeparator ++ (String.repeat decimals "0") | ||
else | ||
let | ||
multiplier : Int | ||
multiplier = | ||
10 ^ decimals | ||
|
||
digits : String | ||
digits = | ||
num | ||
|> (*) (toFloat multiplier) | ||
|> round | ||
|> toString | ||
|
||
intDigits : String | ||
intDigits = | ||
String.dropRight decimals digits | ||
|
||
decDigits : String | ||
decDigits = | ||
String.right decimals digits | ||
in | ||
String.concat | ||
[ addThousandSeparator thousandSeparetor intDigits | ||
, decimalSeparator | ||
, decDigits | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,51 @@ | ||
module Tests exposing (..) | ||
module Tests exposing (all) | ||
|
||
import Test exposing (..) | ||
import Expect | ||
import Documents.Update exposing (..) | ||
import Documents.Update | ||
import Format.Number | ||
|
||
|
||
all : Test | ||
all = | ||
describe "Documents" | ||
describe "Test suite" | ||
[ pagination | ||
, filters | ||
, format | ||
] | ||
|
||
|
||
pagination : Test | ||
pagination = | ||
describe "Documents pagination" | ||
[ test "Documents.totalPages" <| | ||
\() -> | ||
Expect.equal (Documents.Update.totalPages 20) 3 | ||
, test "Documents.onlyDigits" <| | ||
] | ||
|
||
|
||
filters : Test | ||
filters = | ||
describe "Inut filters" | ||
[ test "Documents.onlyDigits" <| | ||
\() -> | ||
Expect.equal (Documents.Update.onlyDigits "12a3") "123" | ||
] | ||
|
||
|
||
format : Test | ||
format = | ||
describe "Format number" | ||
[ test "Format.Number.formatNumber 12345678.90123" <| | ||
\() -> | ||
Expect.equal (Format.Number.formatNumber 2 "." "," 12345678.90123) "12.345.678,90" | ||
, test "Format.Number.formatNumber 3.999" <| | ||
\() -> | ||
Expect.equal (Format.Number.formatNumber 2 "." "," 3.456) "3,46" | ||
, test "Format.Number.formatNumber 1" <| | ||
\() -> | ||
Expect.equal (Format.Number.formatNumber 2 "." "," 1) "1,00" | ||
, test "Format.Number.formatNumber 0.0" <| | ||
\() -> | ||
Expect.equal (Format.Number.formatNumber 2 "." "," 0.0) "0,00" | ||
] |