git.iko.soy/rowtype-yoga/purescript-money
on master help sign in
Add general formatting mechanism
commit8d888e3caa46d6c815a01ed2855ed5032965978a authorrightfold <rightfold@gmail.com> dateThu 30 Mar 2017, 13:07 (10 years ago) parent56f5633
4 files changed+796−171
bower.json+2 src/Data/Finance/Currency.purs+664−166 src/Data/Finance/Money.purs+23−5 src/Data/Finance/Money/Format.purs+107
bower.json+2
@@ -6,9 +6,11 @@
66 "url": "https://github.com/TinkerTravel/purescript-money.git"
77 },
88 "dependencies": {
9+ "purescript-foldable-traversable": "^2.2.0",
910 "purescript-generics": "^3.3.0",
1011 "purescript-globals": "^2.0.0",
1112 "purescript-integers": "^2.1.1",
13+ "purescript-lists": "^3.4.0",
1214 "purescript-math": "^2.0.0",
1315 "purescript-prelude": "^2.5.0",
1416 "purescript-modules": "^2.1.0",
src/Data/Finance/Currency.purs+664−166
@@ -5,6 +5,7 @@ foreign import kind Currency
55data CProxy (c :: Currency) = CProxy
66
77class Currency (c :: Currency) where
8+ code :: CProxy c -> String
89 decimals :: CProxy c -> Int
910
1011foreign import data AED :: Currency
@@ -174,169 +175,666 @@ foreign import data ZAR :: Currency
174175foreign import data ZMW :: Currency
175176foreign import data ZWL :: Currency
176177
177−instance currencyAED :: Currency AED where decimals _ = 2
178−instance currencyAFN :: Currency AFN where decimals _ = 2
179−instance currencyALL :: Currency ALL where decimals _ = 2
180−instance currencyAMD :: Currency AMD where decimals _ = 2
181−instance currencyANG :: Currency ANG where decimals _ = 2
182−instance currencyAOA :: Currency AOA where decimals _ = 2
183−instance currencyARS :: Currency ARS where decimals _ = 2
184−instance currencyAUD :: Currency AUD where decimals _ = 2
185−instance currencyAWG :: Currency AWG where decimals _ = 2
186−instance currencyAZN :: Currency AZN where decimals _ = 2
187−instance currencyBAM :: Currency BAM where decimals _ = 2
188−instance currencyBBD :: Currency BBD where decimals _ = 2
189−instance currencyBDT :: Currency BDT where decimals _ = 2
190−instance currencyBGN :: Currency BGN where decimals _ = 2
191−instance currencyBHD :: Currency BHD where decimals _ = 3
192−instance currencyBIF :: Currency BIF where decimals _ = 0
193−instance currencyBMD :: Currency BMD where decimals _ = 2
194−instance currencyBND :: Currency BND where decimals _ = 2
195−instance currencyBOB :: Currency BOB where decimals _ = 2
196−instance currencyBOV :: Currency BOV where decimals _ = 2
197−instance currencyBRL :: Currency BRL where decimals _ = 2
198−instance currencyBSD :: Currency BSD where decimals _ = 2
199−instance currencyBTN :: Currency BTN where decimals _ = 2
200−instance currencyBWP :: Currency BWP where decimals _ = 2
201−instance currencyBYR :: Currency BYR where decimals _ = 0
202−instance currencyBZD :: Currency BZD where decimals _ = 2
203−instance currencyCAD :: Currency CAD where decimals _ = 2
204−instance currencyCDF :: Currency CDF where decimals _ = 2
205−instance currencyCHE :: Currency CHE where decimals _ = 2
206−instance currencyCHF :: Currency CHF where decimals _ = 2
207−instance currencyCHW :: Currency CHW where decimals _ = 2
208−instance currencyCLF :: Currency CLF where decimals _ = 4
209−instance currencyCLP :: Currency CLP where decimals _ = 0
210−instance currencyCNY :: Currency CNY where decimals _ = 2
211−instance currencyCOP :: Currency COP where decimals _ = 2
212−instance currencyCOU :: Currency COU where decimals _ = 2
213−instance currencyCRC :: Currency CRC where decimals _ = 2
214−instance currencyCUC :: Currency CUC where decimals _ = 2
215−instance currencyCUP :: Currency CUP where decimals _ = 2
216−instance currencyCVE :: Currency CVE where decimals _ = 2
217−instance currencyCZK :: Currency CZK where decimals _ = 2
218−instance currencyDJF :: Currency DJF where decimals _ = 0
219−instance currencyDKK :: Currency DKK where decimals _ = 2
220−instance currencyDOP :: Currency DOP where decimals _ = 2
221−instance currencyDZD :: Currency DZD where decimals _ = 2
222−instance currencyEGP :: Currency EGP where decimals _ = 2
223−instance currencyERN :: Currency ERN where decimals _ = 2
224−instance currencyETB :: Currency ETB where decimals _ = 2
225−instance currencyEUR :: Currency EUR where decimals _ = 2
226−instance currencyFJD :: Currency FJD where decimals _ = 2
227−instance currencyFKP :: Currency FKP where decimals _ = 2
228−instance currencyGBP :: Currency GBP where decimals _ = 2
229−instance currencyGEL :: Currency GEL where decimals _ = 2
230−instance currencyGHS :: Currency GHS where decimals _ = 2
231−instance currencyGIP :: Currency GIP where decimals _ = 2
232−instance currencyGMD :: Currency GMD where decimals _ = 2
233−instance currencyGNF :: Currency GNF where decimals _ = 0
234−instance currencyGTQ :: Currency GTQ where decimals _ = 2
235−instance currencyGYD :: Currency GYD where decimals _ = 2
236−instance currencyHKD :: Currency HKD where decimals _ = 2
237−instance currencyHNL :: Currency HNL where decimals _ = 2
238−instance currencyHRK :: Currency HRK where decimals _ = 2
239−instance currencyHTG :: Currency HTG where decimals _ = 2
240−instance currencyHUF :: Currency HUF where decimals _ = 2
241−instance currencyIDR :: Currency IDR where decimals _ = 2
242−instance currencyILS :: Currency ILS where decimals _ = 2
243−instance currencyINR :: Currency INR where decimals _ = 2
244−instance currencyIQD :: Currency IQD where decimals _ = 3
245−instance currencyIRR :: Currency IRR where decimals _ = 2
246−instance currencyISK :: Currency ISK where decimals _ = 0
247−instance currencyJMD :: Currency JMD where decimals _ = 2
248−instance currencyJOD :: Currency JOD where decimals _ = 3
249−instance currencyJPY :: Currency JPY where decimals _ = 0
250−instance currencyKES :: Currency KES where decimals _ = 2
251−instance currencyKGS :: Currency KGS where decimals _ = 2
252−instance currencyKHR :: Currency KHR where decimals _ = 2
253−instance currencyKMF :: Currency KMF where decimals _ = 0
254−instance currencyKPW :: Currency KPW where decimals _ = 2
255−instance currencyKRW :: Currency KRW where decimals _ = 0
256−instance currencyKWD :: Currency KWD where decimals _ = 3
257−instance currencyKYD :: Currency KYD where decimals _ = 2
258−instance currencyKZT :: Currency KZT where decimals _ = 2
259−instance currencyLAK :: Currency LAK where decimals _ = 2
260−instance currencyLBP :: Currency LBP where decimals _ = 2
261−instance currencyLKR :: Currency LKR where decimals _ = 2
262−instance currencyLRD :: Currency LRD where decimals _ = 2
263−instance currencyLSL :: Currency LSL where decimals _ = 2
264−instance currencyLTL :: Currency LTL where decimals _ = 2
265−instance currencyLYD :: Currency LYD where decimals _ = 3
266−instance currencyMAD :: Currency MAD where decimals _ = 2
267−instance currencyMDL :: Currency MDL where decimals _ = 2
268−instance currencyMGA :: Currency MGA where decimals _ = 2
269−instance currencyMKD :: Currency MKD where decimals _ = 2
270−instance currencyMMK :: Currency MMK where decimals _ = 2
271−instance currencyMNT :: Currency MNT where decimals _ = 2
272−instance currencyMOP :: Currency MOP where decimals _ = 2
273−instance currencyMRO :: Currency MRO where decimals _ = 2
274−instance currencyMUR :: Currency MUR where decimals _ = 2
275−instance currencyMVR :: Currency MVR where decimals _ = 2
276−instance currencyMWK :: Currency MWK where decimals _ = 2
277−instance currencyMXN :: Currency MXN where decimals _ = 2
278−instance currencyMXV :: Currency MXV where decimals _ = 2
279−instance currencyMYR :: Currency MYR where decimals _ = 2
280−instance currencyMZN :: Currency MZN where decimals _ = 2
281−instance currencyNAD :: Currency NAD where decimals _ = 2
282−instance currencyNGN :: Currency NGN where decimals _ = 2
283−instance currencyNIO :: Currency NIO where decimals _ = 2
284−instance currencyNOK :: Currency NOK where decimals _ = 2
285−instance currencyNPR :: Currency NPR where decimals _ = 2
286−instance currencyNZD :: Currency NZD where decimals _ = 2
287−instance currencyOMR :: Currency OMR where decimals _ = 3
288−instance currencyPAB :: Currency PAB where decimals _ = 2
289−instance currencyPEN :: Currency PEN where decimals _ = 2
290−instance currencyPGK :: Currency PGK where decimals _ = 2
291−instance currencyPHP :: Currency PHP where decimals _ = 2
292−instance currencyPKR :: Currency PKR where decimals _ = 2
293−instance currencyPLN :: Currency PLN where decimals _ = 2
294−instance currencyPYG :: Currency PYG where decimals _ = 0
295−instance currencyQAR :: Currency QAR where decimals _ = 2
296−instance currencyRON :: Currency RON where decimals _ = 2
297−instance currencyRSD :: Currency RSD where decimals _ = 2
298−instance currencyRUB :: Currency RUB where decimals _ = 2
299−instance currencyRWF :: Currency RWF where decimals _ = 0
300−instance currencySAR :: Currency SAR where decimals _ = 2
301−instance currencySBD :: Currency SBD where decimals _ = 2
302−instance currencySCR :: Currency SCR where decimals _ = 2
303−instance currencySDG :: Currency SDG where decimals _ = 2
304−instance currencySEK :: Currency SEK where decimals _ = 2
305−instance currencySGD :: Currency SGD where decimals _ = 2
306−instance currencySHP :: Currency SHP where decimals _ = 2
307−instance currencySLL :: Currency SLL where decimals _ = 2
308−instance currencySOS :: Currency SOS where decimals _ = 2
309−instance currencySRD :: Currency SRD where decimals _ = 2
310−instance currencySSP :: Currency SSP where decimals _ = 2
311−instance currencySTD :: Currency STD where decimals _ = 2
312−instance currencySVC :: Currency SVC where decimals _ = 2
313−instance currencySYP :: Currency SYP where decimals _ = 2
314−instance currencySZL :: Currency SZL where decimals _ = 2
315−instance currencyTHB :: Currency THB where decimals _ = 2
316−instance currencyTJS :: Currency TJS where decimals _ = 2
317−instance currencyTMT :: Currency TMT where decimals _ = 2
318−instance currencyTND :: Currency TND where decimals _ = 3
319−instance currencyTOP :: Currency TOP where decimals _ = 2
320−instance currencyTRY :: Currency TRY where decimals _ = 2
321−instance currencyTTD :: Currency TTD where decimals _ = 2
322−instance currencyTWD :: Currency TWD where decimals _ = 2
323−instance currencyTZS :: Currency TZS where decimals _ = 2
324−instance currencyUAH :: Currency UAH where decimals _ = 2
325−instance currencyUGX :: Currency UGX where decimals _ = 0
326−instance currencyUSD :: Currency USD where decimals _ = 2
327−instance currencyUSN :: Currency USN where decimals _ = 2
328−instance currencyUYI :: Currency UYI where decimals _ = 0
329−instance currencyUYU :: Currency UYU where decimals _ = 2
330−instance currencyUZS :: Currency UZS where decimals _ = 2
331−instance currencyVEF :: Currency VEF where decimals _ = 2
332−instance currencyVND :: Currency VND where decimals _ = 0
333−instance currencyVUV :: Currency VUV where decimals _ = 0
334−instance currencyWST :: Currency WST where decimals _ = 2
335−instance currencyXAF :: Currency XAF where decimals _ = 0
336−instance currencyXCD :: Currency XCD where decimals _ = 2
337−instance currencyXOF :: Currency XOF where decimals _ = 0
338−instance currencyXPF :: Currency XPF where decimals _ = 0
339−instance currencyYER :: Currency YER where decimals _ = 2
340−instance currencyZAR :: Currency ZAR where decimals _ = 2
341−instance currencyZMW :: Currency ZMW where decimals _ = 2
342−instance currencyZWL :: Currency ZWL where decimals _ = 2
178+instance currencyAED :: Currency AED where
179+ code _ = "AED"
180+ decimals _ = 2
181+
182+instance currencyAFN :: Currency AFN where
183+ code _ = "AFN"
184+ decimals _ = 2
185+
186+instance currencyALL :: Currency ALL where
187+ code _ = "ALL"
188+ decimals _ = 2
189+
190+instance currencyAMD :: Currency AMD where
191+ code _ = "AMD"
192+ decimals _ = 2
193+
194+instance currencyANG :: Currency ANG where
195+ code _ = "ANG"
196+ decimals _ = 2
197+
198+instance currencyAOA :: Currency AOA where
199+ code _ = "AOA"
200+ decimals _ = 2
201+
202+instance currencyARS :: Currency ARS where
203+ code _ = "ARS"
204+ decimals _ = 2
205+
206+instance currencyAUD :: Currency AUD where
207+ code _ = "AUD"
208+ decimals _ = 2
209+
210+instance currencyAWG :: Currency AWG where
211+ code _ = "AWG"
212+ decimals _ = 2
213+
214+instance currencyAZN :: Currency AZN where
215+ code _ = "AZN"
216+ decimals _ = 2
217+
218+instance currencyBAM :: Currency BAM where
219+ code _ = "BAM"
220+ decimals _ = 2
221+
222+instance currencyBBD :: Currency BBD where
223+ code _ = "BBD"
224+ decimals _ = 2
225+
226+instance currencyBDT :: Currency BDT where
227+ code _ = "BDT"
228+ decimals _ = 2
229+
230+instance currencyBGN :: Currency BGN where
231+ code _ = "BGN"
232+ decimals _ = 2
233+
234+instance currencyBHD :: Currency BHD where
235+ code _ = "BHD"
236+ decimals _ = 3
237+
238+instance currencyBIF :: Currency BIF where
239+ code _ = "BIF"
240+ decimals _ = 0
241+
242+instance currencyBMD :: Currency BMD where
243+ code _ = "BMD"
244+ decimals _ = 2
245+
246+instance currencyBND :: Currency BND where
247+ code _ = "BND"
248+ decimals _ = 2
249+
250+instance currencyBOB :: Currency BOB where
251+ code _ = "BOB"
252+ decimals _ = 2
253+
254+instance currencyBOV :: Currency BOV where
255+ code _ = "BOV"
256+ decimals _ = 2
257+
258+instance currencyBRL :: Currency BRL where
259+ code _ = "BRL"
260+ decimals _ = 2
261+
262+instance currencyBSD :: Currency BSD where
263+ code _ = "BSD"
264+ decimals _ = 2
265+
266+instance currencyBTN :: Currency BTN where
267+ code _ = "BTN"
268+ decimals _ = 2
269+
270+instance currencyBWP :: Currency BWP where
271+ code _ = "BWP"
272+ decimals _ = 2
273+
274+instance currencyBYR :: Currency BYR where
275+ code _ = "BYR"
276+ decimals _ = 0
277+
278+instance currencyBZD :: Currency BZD where
279+ code _ = "BZD"
280+ decimals _ = 2
281+
282+instance currencyCAD :: Currency CAD where
283+ code _ = "CAD"
284+ decimals _ = 2
285+
286+instance currencyCDF :: Currency CDF where
287+ code _ = "CDF"
288+ decimals _ = 2
289+
290+instance currencyCHE :: Currency CHE where
291+ code _ = "CHE"
292+ decimals _ = 2
293+
294+instance currencyCHF :: Currency CHF where
295+ code _ = "CHF"
296+ decimals _ = 2
297+
298+instance currencyCHW :: Currency CHW where
299+ code _ = "CHW"
300+ decimals _ = 2
301+
302+instance currencyCLF :: Currency CLF where
303+ code _ = "CLF"
304+ decimals _ = 4
305+
306+instance currencyCLP :: Currency CLP where
307+ code _ = "CLP"
308+ decimals _ = 0
309+
310+instance currencyCNY :: Currency CNY where
311+ code _ = "CNY"
312+ decimals _ = 2
313+
314+instance currencyCOP :: Currency COP where
315+ code _ = "COP"
316+ decimals _ = 2
317+
318+instance currencyCOU :: Currency COU where
319+ code _ = "COU"
320+ decimals _ = 2
321+
322+instance currencyCRC :: Currency CRC where
323+ code _ = "CRC"
324+ decimals _ = 2
325+
326+instance currencyCUC :: Currency CUC where
327+ code _ = "CUC"
328+ decimals _ = 2
329+
330+instance currencyCUP :: Currency CUP where
331+ code _ = "CUP"
332+ decimals _ = 2
333+
334+instance currencyCVE :: Currency CVE where
335+ code _ = "CVE"
336+ decimals _ = 2
337+
338+instance currencyCZK :: Currency CZK where
339+ code _ = "CZK"
340+ decimals _ = 2
341+
342+instance currencyDJF :: Currency DJF where
343+ code _ = "DJF"
344+ decimals _ = 0
345+
346+instance currencyDKK :: Currency DKK where
347+ code _ = "DKK"
348+ decimals _ = 2
349+
350+instance currencyDOP :: Currency DOP where
351+ code _ = "DOP"
352+ decimals _ = 2
353+
354+instance currencyDZD :: Currency DZD where
355+ code _ = "DZD"
356+ decimals _ = 2
357+
358+instance currencyEGP :: Currency EGP where
359+ code _ = "EGP"
360+ decimals _ = 2
361+
362+instance currencyERN :: Currency ERN where
363+ code _ = "ERN"
364+ decimals _ = 2
365+
366+instance currencyETB :: Currency ETB where
367+ code _ = "ETB"
368+ decimals _ = 2
369+
370+instance currencyEUR :: Currency EUR where
371+ code _ = "EUR"
372+ decimals _ = 2
373+
374+instance currencyFJD :: Currency FJD where
375+ code _ = "FJD"
376+ decimals _ = 2
377+
378+instance currencyFKP :: Currency FKP where
379+ code _ = "FKP"
380+ decimals _ = 2
381+
382+instance currencyGBP :: Currency GBP where
383+ code _ = "GBP"
384+ decimals _ = 2
385+
386+instance currencyGEL :: Currency GEL where
387+ code _ = "GEL"
388+ decimals _ = 2
389+
390+instance currencyGHS :: Currency GHS where
391+ code _ = "GHS"
392+ decimals _ = 2
393+
394+instance currencyGIP :: Currency GIP where
395+ code _ = "GIP"
396+ decimals _ = 2
397+
398+instance currencyGMD :: Currency GMD where
399+ code _ = "GMD"
400+ decimals _ = 2
401+
402+instance currencyGNF :: Currency GNF where
403+ code _ = "GNF"
404+ decimals _ = 0
405+
406+instance currencyGTQ :: Currency GTQ where
407+ code _ = "GTQ"
408+ decimals _ = 2
409+
410+instance currencyGYD :: Currency GYD where
411+ code _ = "GYD"
412+ decimals _ = 2
413+
414+instance currencyHKD :: Currency HKD where
415+ code _ = "HKD"
416+ decimals _ = 2
417+
418+instance currencyHNL :: Currency HNL where
419+ code _ = "HNL"
420+ decimals _ = 2
421+
422+instance currencyHRK :: Currency HRK where
423+ code _ = "HRK"
424+ decimals _ = 2
425+
426+instance currencyHTG :: Currency HTG where
427+ code _ = "HTG"
428+ decimals _ = 2
429+
430+instance currencyHUF :: Currency HUF where
431+ code _ = "HUF"
432+ decimals _ = 2
433+
434+instance currencyIDR :: Currency IDR where
435+ code _ = "IDR"
436+ decimals _ = 2
437+
438+instance currencyILS :: Currency ILS where
439+ code _ = "ILS"
440+ decimals _ = 2
441+
442+instance currencyINR :: Currency INR where
443+ code _ = "INR"
444+ decimals _ = 2
445+
446+instance currencyIQD :: Currency IQD where
447+ code _ = "IQD"
448+ decimals _ = 3
449+
450+instance currencyIRR :: Currency IRR where
451+ code _ = "IRR"
452+ decimals _ = 2
453+
454+instance currencyISK :: Currency ISK where
455+ code _ = "ISK"
456+ decimals _ = 0
457+
458+instance currencyJMD :: Currency JMD where
459+ code _ = "JMD"
460+ decimals _ = 2
461+
462+instance currencyJOD :: Currency JOD where
463+ code _ = "JOD"
464+ decimals _ = 3
465+
466+instance currencyJPY :: Currency JPY where
467+ code _ = "JPY"
468+ decimals _ = 0
469+
470+instance currencyKES :: Currency KES where
471+ code _ = "KES"
472+ decimals _ = 2
473+
474+instance currencyKGS :: Currency KGS where
475+ code _ = "KGS"
476+ decimals _ = 2
477+
478+instance currencyKHR :: Currency KHR where
479+ code _ = "KHR"
480+ decimals _ = 2
481+
482+instance currencyKMF :: Currency KMF where
483+ code _ = "KMF"
484+ decimals _ = 0
485+
486+instance currencyKPW :: Currency KPW where
487+ code _ = "KPW"
488+ decimals _ = 2
489+
490+instance currencyKRW :: Currency KRW where
491+ code _ = "KRW"
492+ decimals _ = 0
493+
494+instance currencyKWD :: Currency KWD where
495+ code _ = "KWD"
496+ decimals _ = 3
497+
498+instance currencyKYD :: Currency KYD where
499+ code _ = "KYD"
500+ decimals _ = 2
501+
502+instance currencyKZT :: Currency KZT where
503+ code _ = "KZT"
504+ decimals _ = 2
505+
506+instance currencyLAK :: Currency LAK where
507+ code _ = "LAK"
508+ decimals _ = 2
509+
510+instance currencyLBP :: Currency LBP where
511+ code _ = "LBP"
512+ decimals _ = 2
513+
514+instance currencyLKR :: Currency LKR where
515+ code _ = "LKR"
516+ decimals _ = 2
517+
518+instance currencyLRD :: Currency LRD where
519+ code _ = "LRD"
520+ decimals _ = 2
521+
522+instance currencyLSL :: Currency LSL where
523+ code _ = "LSL"
524+ decimals _ = 2
525+
526+instance currencyLTL :: Currency LTL where
527+ code _ = "LTL"
528+ decimals _ = 2
529+
530+instance currencyLYD :: Currency LYD where
531+ code _ = "LYD"
532+ decimals _ = 3
533+
534+instance currencyMAD :: Currency MAD where
535+ code _ = "MAD"
536+ decimals _ = 2
537+
538+instance currencyMDL :: Currency MDL where
539+ code _ = "MDL"
540+ decimals _ = 2
541+
542+instance currencyMGA :: Currency MGA where
543+ code _ = "MGA"
544+ decimals _ = 2
545+
546+instance currencyMKD :: Currency MKD where
547+ code _ = "MKD"
548+ decimals _ = 2
549+
550+instance currencyMMK :: Currency MMK where
551+ code _ = "MMK"
552+ decimals _ = 2
553+
554+instance currencyMNT :: Currency MNT where
555+ code _ = "MNT"
556+ decimals _ = 2
557+
558+instance currencyMOP :: Currency MOP where
559+ code _ = "MOP"
560+ decimals _ = 2
561+
562+instance currencyMRO :: Currency MRO where
563+ code _ = "MRO"
564+ decimals _ = 2
565+
566+instance currencyMUR :: Currency MUR where
567+ code _ = "MUR"
568+ decimals _ = 2
569+
570+instance currencyMVR :: Currency MVR where
571+ code _ = "MVR"
572+ decimals _ = 2
573+
574+instance currencyMWK :: Currency MWK where
575+ code _ = "MWK"
576+ decimals _ = 2
577+
578+instance currencyMXN :: Currency MXN where
579+ code _ = "MXN"
580+ decimals _ = 2
581+
582+instance currencyMXV :: Currency MXV where
583+ code _ = "MXV"
584+ decimals _ = 2
585+
586+instance currencyMYR :: Currency MYR where
587+ code _ = "MYR"
588+ decimals _ = 2
589+
590+instance currencyMZN :: Currency MZN where
591+ code _ = "MZN"
592+ decimals _ = 2
593+
594+instance currencyNAD :: Currency NAD where
595+ code _ = "NAD"
596+ decimals _ = 2
597+
598+instance currencyNGN :: Currency NGN where
599+ code _ = "NGN"
600+ decimals _ = 2
601+
602+instance currencyNIO :: Currency NIO where
603+ code _ = "NIO"
604+ decimals _ = 2
605+
606+instance currencyNOK :: Currency NOK where
607+ code _ = "NOK"
608+ decimals _ = 2
609+
610+instance currencyNPR :: Currency NPR where
611+ code _ = "NPR"
612+ decimals _ = 2
613+
614+instance currencyNZD :: Currency NZD where
615+ code _ = "NZD"
616+ decimals _ = 2
617+
618+instance currencyOMR :: Currency OMR where
619+ code _ = "OMR"
620+ decimals _ = 3
621+
622+instance currencyPAB :: Currency PAB where
623+ code _ = "PAB"
624+ decimals _ = 2
625+
626+instance currencyPEN :: Currency PEN where
627+ code _ = "PEN"
628+ decimals _ = 2
629+
630+instance currencyPGK :: Currency PGK where
631+ code _ = "PGK"
632+ decimals _ = 2
633+
634+instance currencyPHP :: Currency PHP where
635+ code _ = "PHP"
636+ decimals _ = 2
637+
638+instance currencyPKR :: Currency PKR where
639+ code _ = "PKR"
640+ decimals _ = 2
641+
642+instance currencyPLN :: Currency PLN where
643+ code _ = "PLN"
644+ decimals _ = 2
645+
646+instance currencyPYG :: Currency PYG where
647+ code _ = "PYG"
648+ decimals _ = 0
649+
650+instance currencyQAR :: Currency QAR where
651+ code _ = "QAR"
652+ decimals _ = 2
653+
654+instance currencyRON :: Currency RON where
655+ code _ = "RON"
656+ decimals _ = 2
657+
658+instance currencyRSD :: Currency RSD where
659+ code _ = "RSD"
660+ decimals _ = 2
661+
662+instance currencyRUB :: Currency RUB where
663+ code _ = "RUB"
664+ decimals _ = 2
665+
666+instance currencyRWF :: Currency RWF where
667+ code _ = "RWF"
668+ decimals _ = 0
669+
670+instance currencySAR :: Currency SAR where
671+ code _ = "SAR"
672+ decimals _ = 2
673+
674+instance currencySBD :: Currency SBD where
675+ code _ = "SBD"
676+ decimals _ = 2
677+
678+instance currencySCR :: Currency SCR where
679+ code _ = "SCR"
680+ decimals _ = 2
681+
682+instance currencySDG :: Currency SDG where
683+ code _ = "SDG"
684+ decimals _ = 2
685+
686+instance currencySEK :: Currency SEK where
687+ code _ = "SEK"
688+ decimals _ = 2
689+
690+instance currencySGD :: Currency SGD where
691+ code _ = "SGD"
692+ decimals _ = 2
693+
694+instance currencySHP :: Currency SHP where
695+ code _ = "SHP"
696+ decimals _ = 2
697+
698+instance currencySLL :: Currency SLL where
699+ code _ = "SLL"
700+ decimals _ = 2
701+
702+instance currencySOS :: Currency SOS where
703+ code _ = "SOS"
704+ decimals _ = 2
705+
706+instance currencySRD :: Currency SRD where
707+ code _ = "SRD"
708+ decimals _ = 2
709+
710+instance currencySSP :: Currency SSP where
711+ code _ = "SSP"
712+ decimals _ = 2
713+
714+instance currencySTD :: Currency STD where
715+ code _ = "STD"
716+ decimals _ = 2
717+
718+instance currencySVC :: Currency SVC where
719+ code _ = "SVC"
720+ decimals _ = 2
721+
722+instance currencySYP :: Currency SYP where
723+ code _ = "SYP"
724+ decimals _ = 2
725+
726+instance currencySZL :: Currency SZL where
727+ code _ = "SZL"
728+ decimals _ = 2
729+
730+instance currencyTHB :: Currency THB where
731+ code _ = "THB"
732+ decimals _ = 2
733+
734+instance currencyTJS :: Currency TJS where
735+ code _ = "TJS"
736+ decimals _ = 2
737+
738+instance currencyTMT :: Currency TMT where
739+ code _ = "TMT"
740+ decimals _ = 2
741+
742+instance currencyTND :: Currency TND where
743+ code _ = "TND"
744+ decimals _ = 3
745+
746+instance currencyTOP :: Currency TOP where
747+ code _ = "TOP"
748+ decimals _ = 2
749+
750+instance currencyTRY :: Currency TRY where
751+ code _ = "TRY"
752+ decimals _ = 2
753+
754+instance currencyTTD :: Currency TTD where
755+ code _ = "TTD"
756+ decimals _ = 2
757+
758+instance currencyTWD :: Currency TWD where
759+ code _ = "TWD"
760+ decimals _ = 2
761+
762+instance currencyTZS :: Currency TZS where
763+ code _ = "TZS"
764+ decimals _ = 2
765+
766+instance currencyUAH :: Currency UAH where
767+ code _ = "UAH"
768+ decimals _ = 2
769+
770+instance currencyUGX :: Currency UGX where
771+ code _ = "UGX"
772+ decimals _ = 0
773+
774+instance currencyUSD :: Currency USD where
775+ code _ = "USD"
776+ decimals _ = 2
777+
778+instance currencyUSN :: Currency USN where
779+ code _ = "USN"
780+ decimals _ = 2
781+
782+instance currencyUYI :: Currency UYI where
783+ code _ = "UYI"
784+ decimals _ = 0
785+
786+instance currencyUYU :: Currency UYU where
787+ code _ = "UYU"
788+ decimals _ = 2
789+
790+instance currencyUZS :: Currency UZS where
791+ code _ = "UZS"
792+ decimals _ = 2
793+
794+instance currencyVEF :: Currency VEF where
795+ code _ = "VEF"
796+ decimals _ = 2
797+
798+instance currencyVND :: Currency VND where
799+ code _ = "VND"
800+ decimals _ = 0
801+
802+instance currencyVUV :: Currency VUV where
803+ code _ = "VUV"
804+ decimals _ = 0
805+
806+instance currencyWST :: Currency WST where
807+ code _ = "WST"
808+ decimals _ = 2
809+
810+instance currencyXAF :: Currency XAF where
811+ code _ = "XAF"
812+ decimals _ = 0
813+
814+instance currencyXCD :: Currency XCD where
815+ code _ = "XCD"
816+ decimals _ = 2
817+
818+instance currencyXOF :: Currency XOF where
819+ code _ = "XOF"
820+ decimals _ = 0
821+
822+instance currencyXPF :: Currency XPF where
823+ code _ = "XPF"
824+ decimals _ = 0
825+
826+instance currencyYER :: Currency YER where
827+ code _ = "YER"
828+ decimals _ = 2
829+
830+instance currencyZAR :: Currency ZAR where
831+ code _ = "ZAR"
832+ decimals _ = 2
833+
834+instance currencyZMW :: Currency ZMW where
835+ code _ = "ZMW"
836+ decimals _ = 2
837+
838+instance currencyZWL :: Currency ZWL where
839+ code _ = "ZWL"
840+ decimals _ = 2
src/Data/Finance/Money.purs+23−5
@@ -1,13 +1,18 @@
11module Data.Finance.Money
22 ( Discrete(..)
3+ , formatDiscrete
34 , showDiscrete
45 ) where
56
6−import Data.Finance.Currency (kind Currency, class Currency, CProxy(..), decimals)
7+import Data.Finance.Currency (kind Currency, class Currency, CProxy(..))
8+import Data.Finance.Currency as Currency
9+import Data.Finance.Money.Format (Format, FormatF(..), absolute, ifNegative, literal)
10+import Data.Foldable (foldMap)
711import Data.Generic (class Generic, gShow)
812import Data.Int as Int
913import Data.Module (class LeftModule, class RightModule)
1014import Data.Newtype (class Newtype)
15+import Data.Ord (abs)
1116import Global.Unsafe (unsafeToFixed)
1217import Math (pow)
1318import Prelude
@@ -35,6 +40,21 @@ instance rightModuleDiscrete :: RightModule (Discrete c) Int where
3540 msubR (Discrete a) (Discrete b) = Discrete (a - b)
3641 mmulR (Discrete a) b = Discrete (a * b)
3742
43+-- | Apply a format to a discrete amount.
44+formatDiscrete :: ∀ c. Currency c => Format -> Discrete c -> String
45+formatDiscrete f (Discrete n) = foldMap go f
46+ where
47+ go (IfNegative s) = if n < 0 then foldMap go s else ""
48+ go (Literal s) = s
49+ go CurrencyCode = Currency.code (CProxy :: CProxy c)
50+ go Absolute =
51+ unsafeToFixed d (n' / pow 10.0 (Int.toNumber d))
52+ where
53+ n' = abs $ Int.toNumber n
54+ d = Currency.decimals (CProxy :: CProxy c)
55+
56+-- | DEPRECATED: use `formatDiscrete` instead.
57+-- |
3858-- | Show the discrete value with the correct number of decimals. Will not
3959-- | prepend the currency sign. Negative amounts are prefixed with a
4060-- | hyphen-minus.
@@ -43,7 +63,5 @@ instance rightModuleDiscrete :: RightModule (Discrete c) Int where
4363-- |
4464-- | - `showDiscrete (Discrete 256 :: Discrete GBP) == "2.56"`
4565-- | - `showDiscrete (Discrete (-256) :: Discrete GBP) == "-2.56"`
46−showDiscrete :: ∀ c. (Currency c) => Discrete c -> String
47−showDiscrete (Discrete n) =
48− unsafeToFixed d (Int.toNumber n / pow 10.0 (Int.toNumber d))
49− where d = decimals (CProxy :: CProxy c)
66+showDiscrete :: ∀ c. Currency c => Discrete c -> String
67+showDiscrete = formatDiscrete $ ifNegative (literal "-") <> absolute
src/Data/Finance/Money/Format.pursnew file+107
@@ -0,0 +1,107 @@
1+module Data.Finance.Money.Format
2+ ( Format
3+ , FormatF(..)
4+ , ifNegative
5+ , literal
6+ , currencyCode
7+ , absolute
8+
9+ , sbind
10+
11+ , numeric
12+ , numericC
13+ , accountant
14+ , accountantC
15+ ) where
16+
17+import Data.List (List)
18+import Prelude
19+
20+--------------------------------------------------------------------------------
21+
22+-- | Format specification for amounts.
23+type Format = List FormatF
24+
25+-- | Part of a format specification for amounts.
26+data FormatF
27+ = IfNegative Format
28+ | Literal String
29+ | CurrencyCode
30+ | Absolute
31+
32+-- | Perform a format only if the amount is negative.
33+ifNegative :: Format -> Format
34+ifNegative = pure <<< IfNegative
35+
36+-- | Insert a literal string.
37+literal :: String -> Format
38+literal = pure <<< Literal
39+
40+-- | Insert the currency code.
41+currencyCode :: Format
42+currencyCode = pure CurrencyCode
43+
44+-- | Insert the absolute value of the amount in decimal.
45+absolute :: Format
46+absolute = pure Absolute
47+
48+--------------------------------------------------------------------------------
49+
50+-- | Utility function so you can define formats with do notation. For example:
51+-- |
52+-- | ```purescript
53+-- | myFormat = do
54+-- | ifNegative $ literal "("
55+-- | currencyCode
56+-- | literal space
57+-- | amount
58+-- | ifNegative $ literal ")"
59+-- | where bind = sbind
60+-- | ```
61+sbind :: ∀ a. Semigroup a => a -> (Unit -> a) -> a
62+sbind = flip $ flip append <<< (_ $ unit)
63+
64+--------------------------------------------------------------------------------
65+
66+-- | Numeric format, with a minus sign for negative amounts, and no currency
67+-- | indicator.
68+numeric :: Format
69+numeric = ifNegative (literal minus) <> absolute
70+
71+-- | Numeric format, with a minus sign for negative amounts, and a currency
72+-- | code.
73+numericC :: Format
74+numericC = do
75+ ifNegative $ literal minus
76+ currencyCode
77+ literal space
78+ absolute
79+ where bind = sbind
80+
81+-- | Accountant format, with negative amounts parenthesized, and no currency
82+-- | code.
83+accountant :: Format
84+accountant = do
85+ ifNegative $ literal "("
86+ absolute
87+ ifNegative $ literal ")"
88+ where bind = sbind
89+
90+-- | Accountant format, with negative amounts parenthesized, and a currency
91+-- | code.
92+accountantC :: Format
93+accountantC = do
94+ ifNegative $ literal "("
95+ currencyCode
96+ literal space
97+ absolute
98+ ifNegative $ literal ")"
99+ where bind = sbind
100+
101+--------------------------------------------------------------------------------
102+
103+minus :: String
104+minus = "\x2212"
105+
106+space :: String
107+space = " "