Can you suggest a best way to define money type in F#?
-
1Judging from the answers, your question needs clarifying. Are you talking about units of measure (e.g. for not confusing two different currencies), or about decimals and rounding problems? – Benjol May 25 '09 at 08:34
5 Answers
Always, always, use System.Decimal for storing financial data! (Unless you dont care about inaccuracy and rounding errors!) https://learn.microsoft.com/dotnet/api/system.decimal
-
So F# measures don't provide any additional benefits to financial applications? – Sergej Andrejev May 21 '09 at 14:10
-
1Yes, but you still can define a decimal
(or whatever currency measure you create) – H27studio Apr 10 '15 at 19:00
type money = int<dollars>
?
Haven't even tried it to see... can you define arbitrary units, or does it only work with explicitly defined ones?
Obviously you'd probably want
type money = int<thousandths_of_currency>
(or tens of pennies, or whatever).
To be more accurate.
edited:
decimals take types so you can define money as:
[<Measure>]
type = pounds
type money = decimal<pounds>
which could ensure currencies aren't cross converted by accident, eg:
if
balance = decimal<pounds>
and
payment = decimal<dollars>
newbalance = balance + payment
will not compile, and you'll have to convert payment to decimal<pounds>

- 4,327
- 23
- 25
-
I don't get what is your suggestion :( F# allows to define arbitrary units. – Sergej Andrejev May 21 '09 at 14:07
-
F# allows you to define units of measure for a number type. So you can define a type length = float
and the units will be preserved through calculations. See: http://blogs.msdn.com/andrewkennedy/archive/tags/Units+of+Measure/default.aspx – Massif May 21 '09 at 14:24 -
1But, joy of joys.. you can define units of measure onto decimals! [
] type = dollars type money = decimal – Massif May 21 '09 at 14:35 -
As of May 2009 CTP, you can apparently also apply units of measure to ints (though what help this would be for money, I'm not sure). – Benjol May 25 '09 at 08:33
Luca Bolognese suggests one defines their own Money type based off of float:
[<Measure>] type money
let money (f:float) = f * 1.<money>

- 17,793
- 14
- 58
- 60
-
I followed the link, and that's an example, not a suggestion. It depends on the application, but I believe for the large majority of applications the accepted answer is the way to go. – Bent Tranberg Apr 03 '16 at 19:40
F# now has built in support for Measures and Units. According to the lead engineer for this feature, Kennedy it is aimed at financial apps among other solutions.
So I would look at that before defining my own money type in F#.
Werner

- 11
- 1
use a long, and store pennies (or tenths of a penny) in it.
You could use a class like Decimal, but that usually ends up being quite slow.
-
1shouldn't correctness be more important than speed in this case? – Erich Mirabal May 21 '09 at 14:13
-
how is using an integer type to store the smallest unit not accurate. Its not like using a double type. – gbjbaanb May 21 '09 at 14:42
-
2Dangerous. Besides the high chance of error, you can later have problems when someone says the tax rate is $0.001 per unit. – Jonathan Allen Jun 04 '09 at 00:32
-
1I voted for this because I use a cent unit of measure and int. for my simple financial needs this works out great. I think the money type needs to be defined based on the requirements of the application. In my scenario, I can say with 99.9% confidence that I will never be using tenths of a cent – Brad Feb 09 '12 at 17:51