- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
No, if you're using Java, as this example is, then the answer is just DON'T.
BigDecimal does everything you need, including numerous rounding options, one of which will be legally required for financial transactions, depending where you are in the world (e.g in Europe halves are rounded up when needed, but I believe the USA rounds down).
I've seen numerous applications where this wasn't done, and watched with amusement as customers of (and subsequently management within) reputable finance industry companies went crazy as they started seeing 999.99 instead of 1000 and questions of trustworthiness got raised.
However it's usually fairly quick to fix once you've identified where all the floats are in the code, and sent the developer(s) responsible one of the many web articles on why you MUST use BigDecimal in Java for this kind of work.
Admin
But sometimes money is "sort of floating", as when you're working with financial amounts that can be in a lot of different currencies but are using the French (aka ISO) standard for representing them.
Dollars of various sorts (US, Canadian, Australian, Hong Kong, Taiwan) have two decimal places, as do British pounds, Euros, Mexican pesos, Japanese yen (although nobody actually uses sen), etc. Others such as Thai baht, Indian rupees, the now-defunct Italian lira (which were still funct back when I had to deal with this stuff), are un-subdivided, i.e., zero decimal places. A few Middle-Eastern things like Bahraini or Libyan dinar break down into thousandths so you need three decimals.
I searched long and hard for any place breaking their currency into tenths so I could comprehensively test the logic that allowed from zero to three decimals, but it appears they don't exist. Neither (since the Brits decimalised the pound) does anyone break down their simoleons into other than decimal fractions.
Admin
Admin
It looks like it's reading/writing a flat file that's an interface to some legacy system that requires the data in that format, e.g. something COBOL based.
Admin
Historically in Japan there was 1 rin (one-thousandth) and during World War II there was still 5 rin (five one-thousandths). Conscripted soldiers were referred to as issen gorin because postage for a conscription notice was 1 sen 5 rin. Fractions of a yen are no longer legal tender. Sometimes accounting uses them; for example a pharmacy might account for 50 pills at 8.45 yen each and then truncate the total, but a bank passbook doesn't show any fractions.
I have some Thai coins for 25 satang (25 hundredths of a baht) and Indian coins for some quantities of paises (hundredths of a rupee), though I don't recall seeing fractions in prices.
By the way, in the other direction, quadrillion is used in measuring Japan's debt.
Admin
Admin
Or to put it a bit more succinctly: floating point ain't worth a dime.
Admin
Admin
Yeah, it's time to retire the Reply button.
Admin
And I have bought stock with an accuracy at a ten-thousandth of a dollar. But I think putting an integer (4-byte, unsigned) for a fraction of a dollar is sufficient for most use-cases. And having it wrapped as a data structure beforehand makes it easier to extend in the future to something with a higher range (8, 16-bytes, dynamic scaling).
But not trusting double and then using 3 of them seems brillant.
Admin
Admin
Actually it's Java (note the case of String and toString).
So yes, TRWTF is using double instead of BigDecimal.
Admin
Admin
Admin
Never seemed to cause any issues with my credit card companies, but the presentation looked decidedly shady.
Admin
beancounters gone wild... ... beancounters gone programming
...quick hide your kids, hide your wife ...
captcha: nobis (the latin of noob)
Admin
Admin
So le BigMac weights, erm costs 15 stone?
Admin
..and expected Google to answer: "Do you mean an imperial gallon or a us gallon?"
Admin
Captcha "transverbero" TRWTF is long captchas. I prefer "nibh"
Admin
TRWTF is that you still use a floating point data type (double)
Admin
That would be determined by the localization/localisation settings of your Google account.
Admin
Of course the number of litres is different too, but that's a different difference.
Admin
And indeed how could I forget. Reentry cards for foreigners to depart Japan temporarily with intent to return have a box where we have to fill in the amount of money we're carrying. One numeral. One total amount. After that in parentheses is a list, yen, dollars, etc. So I circle the word for yen and the word for dollars and the word for etc., count everything and add them all up, and put the total in the box. No problem. So one time I carried a Zimbabwe one hundred trillion dollar bill along with some US dollars and yen, added everything up, and wrote the total in the box. No problem.
Admin
When advertising a price per unit you can specify it in whatever precision you please, but the actual exchange of money and thus the amount of money in any given account is never more precise than pennies.
So you never have a reason to worry about someone else using a more precise value than pennies.
Admin
Let me guess: they ported it from COBOL.