Live data from Hacker News

Your balance is $0.30000000004

medium.com

31–40 of 69 posts

Re: Your balance is $0.30000000004

#31
post #27

I'm currently rewriting our store implementation, and it's complicated. One example is that some currencies have 100 subunits (eg USD), some have 10 subunits (EG TWD/MOP) and some have no subunits (eg JPY). This is made slightly more difficult in that Stripe/Paypal will treat subunits differently, for example Paypal might treat JPY as having 100 subunits and Stripe will treat it with 0 subunits. You want to be really…

What's the long term benefit?

Lots of third party implementations will take a few % off your bottom line. That's the main one.

There's a couple of other points here as well:

- If you do enough volume, you're able to negotiate the fee with the payment processor, we've done this successfully. If you outsource, that negotiation is the third parties margin.

- I'm not sure if this is true for third parties, but if they wrap up all your payouts in your native currency (EG GBP£), you lose money on forex. We try our best to have all our EUR and USD sales land in their respective currency accounts, then convert them with low cost services such as transferwise which has saved us a lot of money in the long run.

Saving on your forex and having no third party processor fees, I think in some instances you could be saving in the region of £40,000 to £80,000 per £1mm of sales.

We also couldn't find a third party implementation that would allow us to bill for our product, varied by country, amount, currency AND billing frequency. This fine tuned control can have real benefit, for example we noticed in Ukraine we had high traffic but no sales - by introducing a low monthly UAH billing option we started to grow sales in this region.

Other third party implementations won't handle VAT properly or comprehensively which can create more of an accounting burden, or round VAT transactions unfavourably (size of that benefit is irrelevant to very small obviously depending on volume of transactions!) This gap is being slowly filled from what I've been seeing though.

Using a third party solution for SaaS will always have some level of disconnect, it just feels nicer for the customer when it's all integrated seamlessly. One example being you have full control over branding and distribution of invoices/receipts etc.

Re: Your balance is $0.30000000004

#32

Work with cents everywhere and format the output in the UI. Done.

The smallest unit of US money is the mill or 1/1000 of a dollar. The smallest unit of US currency is the cent. You're supposed to work in mills and round to cents. Or just work in floats and round as the last step. Or do both selectively depending on which rounding error works in your favor, but don't tell anyone that's what you're doing. The real problem is that nobody at the company seems to have looked at the part…

I really don't see any need or benefit in storing values as mills unless you're writing forex trading platforms. It's inviting more mistakes.

Re: Your balance is $0.30000000004

#33
One of the reasons people used COBOL is that it handled currency better. You'd say "ACCOUNT-BALANCE PIC S999999V99" to specify the account balance was signed with 8 digits, and an implied decimal point (V) before the last two. The math was done with integers so floating point inaccuracy wasn't a problem.

An unrelated issue with money handling: I once got a credit card charge for $NaN (not a number). Fortunately it was resolved without me needing to write a check for $0/0. Photo of the receipt at http://www.righto.com/2008/05/importance-of-software-testing...

Re: Your balance is $0.30000000004

#34
post #10

Earlier quoted context omitted.

browser console: (1.005).toFixed(2) (1.005).toFixed(20) reveals the problem. Math.round(1.005 * 100) // wrong In the end these conversion errors are not solvable in any language, so you have to "cut off" somewhere. There are different approaches to this. Wasn't there a case where programmers stole the "wrong" cent and wasn't The Office a persiflage on that?

That was the main plot of Office Space. https://www.imdb.com/title/tt0151804/

And SuperMan 3 https://www.imdb.com/title/tt0086393/?ref_=fn_al_tt_1

Re: Your balance is $0.30000000004

#35
post #22
post #2

You would think that this is a fairly rookie error and that big companies would know better, but I regularly see this on Uber: https://i.imgur.com/qDACtG0.png

Uber does not use float for money but the internal representation is converted to something displayable in the API layer. Normally strings are used, for example: https://developer.uber.com/docs/riders/references/api/v1.2/r... Guess somebody messed up for this case.

In this case, the value is the "surge_multiplier", which is indeed a float: https://developer.uber.com/docs/riders/references/api/v1.2/e...

Re: Your balance is $0.30000000004

#36
post #22
post #2

You would think that this is a fairly rookie error and that big companies would know better, but I regularly see this on Uber: https://i.imgur.com/qDACtG0.png

Uber does not use float for money but the internal representation is converted to something displayable in the API layer. Normally strings are used, for example: https://developer.uber.com/docs/riders/references/api/v1.2/r... Guess somebody messed up for this case.

Interestingly even in your link you can see that most of the money values (subtotal, total_fare, total_charged) are strings but for some reason total_owed is a float.

Presumably someone messed up but it wasn't caught in code review.

Re: Your balance is $0.30000000004

#37

Martin Fowler's money pattern [0] solves this problem. Just make sure whenever you're dealing with currencies to use a value object for money, rather than float. Lots of languages have libraries for this pattern. [1] [0] - https://www.martinfowler.com/eaaCatalog/money.html [1] - https://github.com/topics/fowler-money-pattern

It looks like the amount is stored as an int, which could overflow when dealing with Bitcoin if you're using Satoshi's as a subunit.

Re: Your balance is $0.30000000004

#38
post #30

Martin Fowler's money pattern [0] solves this problem. Just make sure whenever you're dealing with currencies to use a value object for money, rather than float. Lots of languages have libraries for this pattern. [1] [0] - https://www.martinfowler.com/eaaCatalog/money.html [1] - https://github.com/topics/fowler-money-pattern

One thing I've always wondered is who the heck is Martin Fowler and why does he have such a following?

He wrote influential software engineering textbooks on patterns, agile development, refactoring and the like. I'm not sure how relevant he is today, but in the 90s the ideas he presented were pretty groundbreaking.

Re: Your balance is $0.30000000004

#39
post #18
post #16

Earlier quoted context omitted.

This approach doesn't work when dealing with fractional cents, like charging $0.0001 per hour for something. I would rather recommend using an arbitrary precision decimal library like Decimal.js, and using strings to represent money in JSON. And of course using an appropriate database type for storing the data.

Just make the smallest possible fractional cent the unit then

What if you can't know that beforehand? E.g. if you suddenly need to charge $0.000000001 per second.

Re: Your balance is $0.30000000004

#40
post #30

Martin Fowler's money pattern [0] solves this problem. Just make sure whenever you're dealing with currencies to use a value object for money, rather than float. Lots of languages have libraries for this pattern. [1] [0] - https://www.martinfowler.com/eaaCatalog/money.html [1] - https://github.com/topics/fowler-money-pattern

One thing I've always wondered is who the heck is Martin Fowler and why does he have such a following?

I recommend his blog. He was, and I think might still be, ahead of the curve regarding software engineering patterns etc. There was an article shared on HN which hit #1 from a startups tech blog talking about their brand new architecture and someone pointed out MF wrote about an identical architecture pattern in 2004 or something.
Post reply on HN