git.iko.soy/rowtype-yoga/purescript-money
on master help sign in
Rewrite showDiscrete' in PureScript

Fixes #2.

commit10dd1afb8f3f6292eacf2c5ccd6f9b02e5beb1a4 authorrightfold <rightfold@gmail.com> dateThu 30 Mar 2017, 11:45 (10 years ago) parent4901d10
3 files changed+12−10
bower.json+3 src/Data/Finance/Money.js−7 src/Data/Finance/Money.purs+9−3
bower.json+3
@@ -7,6 +7,9 @@
77 },
88 "dependencies": {
99 "purescript-generics": "^3.3.0",
10+ "purescript-globals": "^2.0.0",
11+ "purescript-integers": "^2.1.1",
12+ "purescript-math": "^2.0.0",
1013 "purescript-prelude": "^2.5.0",
1114 "purescript-modules": "^2.1.0",
1215 "purescript-newtype": "^1.3.0"
src/Data/Finance/Money.jsdeleted−7
@@ -1,7 +0,0 @@
1−'use strict';
2−
3−exports['showDiscrete\''] = function(n) {
4− return function(d) {
5− return (n / Math.pow(10, d)).toFixed(d);
6− };
7−};
src/Data/Finance/Money.purs+9−3
@@ -5,8 +5,11 @@ module Data.Finance.Money
55
66import Data.Finance.Currency (kind Currency, class Currency, CProxy(..), decimals)
77import Data.Generic (class Generic, gShow)
8+import Data.Int as Int
89import Data.Module (class LeftModule, class RightModule)
910import Data.Newtype (class Newtype)
11+import Global.Unsafe (unsafeToFixed)
12+import Math (pow)
1013import Prelude
1114
1215-- | An amount of money in the smallest discrete unit of a particular currency.
@@ -32,7 +35,10 @@ instance rightModuleDiscrete :: RightModule (Discrete c) Int where
3235 msubR (Discrete a) (Discrete b) = Discrete (a - b)
3336 mmulR (Discrete a) b = Discrete (a * b)
3437
38+-- | Show the discrete value with the correct number of decimals. Will not
39+-- | prepend the currency sign. Negative numbers are prefixed with a
40+-- | hyphen-minus.
3541showDiscrete :: ∀ c. (Currency c) => Discrete c -> String
36−showDiscrete (Discrete n) = showDiscrete' n (decimals (CProxy :: CProxy c))
37−
38−foreign import showDiscrete' :: Int -> Int -> String
42+showDiscrete (Discrete n) =
43+ unsafeToFixed d (Int.toNumber n / pow 10.0 (Int.toNumber d))
44+ where d = decimals (CProxy :: CProxy c)