aboutsummaryrefslogtreecommitdiff
path: root/src/Format.purs
diff options
context:
space:
mode:
authorJoris2017-04-05 17:04:33 +0200
committerJoris2017-04-05 17:04:33 +0200
commit9a95a674fbbf1e64d3ad07922d569c3a1c751cf2 (patch)
tree2f99889040b0af406df1c403b9d930fe77e706cf /src/Format.purs
parentf6a73e5bd6a5e2d7d4eb9c8a14bdf1a0c8a4ac4c (diff)
Show aliments, glycemic index, carbohydrates and glycemic charge
Diffstat (limited to 'src/Format.purs')
-rw-r--r--src/Format.purs29
1 files changed, 29 insertions, 0 deletions
diff --git a/src/Format.purs b/src/Format.purs
new file mode 100644
index 0000000..22593e5
--- /dev/null
+++ b/src/Format.purs
@@ -0,0 +1,29 @@
+module Format where
+
+import Data.Int (toNumber, fromNumber)
+import Data.Array (replicate)
+import Data.Maybe (fromMaybe)
+import Data.String (length, joinWith)
+import Math (round, trunc, pow)
+
+import Prelude
+
+number :: Int -> Number -> String
+number decimalLength num = formattedIntegerPart <> formattedDecimalPart
+ where
+ formattedIntegerPart =
+ (if decimalLength > 0 then trunc num else round num)
+ # fromNumber
+ # fromMaybe 0
+ # show
+
+ formattedDecimalPart =
+ if decimalLength > 0 then
+ ((num - trunc num) * pow 10.0 (toNumber decimalLength))
+ # round
+ # fromNumber
+ # fromMaybe 0
+ # show
+ # \str -> "," <> (joinWith "" $ replicate (decimalLength - length str) "0") <> str
+ else
+ ""