Toy program for dividing monetary values

Hey all,

I recently stumbled upon this article about dealing with monetary values in software.

It had a short code example that I wanted to try out for fun. The code example shown attempts to divide a sum of money as fairly as possible. I expanded the example a bit so that it can work with decimal numbers, such as $19.99 or 19.99 € instead of just integer values.


Thanks for this @art-rasa. I fixed up LFortran to be able to compile it. Everything now seems to work except the read statement is not yet implemented in the backend, for now I just assigned the values directly in the source code, then it seems to run, so I am very happy about it. We’ll implement the read statement soon.


Recent discussions around “string to floating-point” utility are relevant to this example. Having such a utility, either as pure Fortran in Fortran stdlib or as an interface to some other standard libraries such as those with C, can be useful when Fortranners want to try out such programs.


Thanks for testing the code.

The only other programming language I’m familiar with is C, and in the past I’ve used the atof ( function for string to floating point conversions.

What I learned from my toy program: Representing money in software is quite a complex topic. It’s a can of worms!

1 Like

Python, with its decimal class that has good precision even if its range is lower than binary floating-point representation of similar storage, likely provides a better type to handle the arithmetic of monetary transactions than what the readily available processors offer with Fortran.

1 Like

Can a Fortran compiler provide a “decimal” type as one of the kinds for real?