- 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
The result is actually wrong, they need to divide by 100. Also, you really need to avoid using doubles when dealing with money otherwise you may lose precision. You should use BigDecimal, which may be a bit slower, but would at least guarantee that your number never loses precision.
new BigDecimal(amount).divide(new BigDecimal( 100))
Admin
Better example
new BigDecimal(amount).movePointLeft(2)
Admin
Admin
Commented-out debugging code in a production system is a big red flag also. Usually, this feature appears in code written by someone who had to use a hammer to beat the code into shape, instead of code written by someone who could do it right the first time.
Admin
Pain in the ass? Tell me, what is easier than reading a known-length string from a file? Using delimiters that need to be escaped? Writing an XML parser and a DTD for your file format?
Don't get me wrong, fixed length data files certainly have issues, the most obvious being that the fields are limited in length, have no context for human conversion, and are difficult to extend.
But if anything, they are easy as sin to parse. Whatever (reasonable) language you are using, it can read a fixed-length string and parse it some other basic data type. Does your language give you an XML parser? What about CSV? Which of these formats does your favorite RDBMS use to store its data?
So, in short, the reasoning is that, limitations aside, fixed length data files are easier than other formats to write and to parse by such an order of magnitude that, for general use, they are the only viable option.
Admin
Hey! He copied & pasted this thing into 122 different places. If that isn't heavy reuse, I don't know what is. :-)
He even reused the bugs.
Admin
Admin
amount = "000000028000"; amount = String.format("%0.2f", Integer.parseInt(amount)*1e-2);
(java)
Admin
Admin
Good, I hate poodles!
captcha: scooter
Admin
Since I have nothing noteworthy to comment about, such as "the REAL wtf is..." or what captcha I had to type in, I'll just agree with the author that this code is indeed invaluable.
Admin
Yes, Google knows alright. Definitly.
Admin
Thank you, you pointed it out to me. Without this break, the code will actually get stuck in an infinite loop continually adding additional characters to the string until it overflows.
Admin
First year java students could figure this out. Especially Lazy ones. This must have been written by someone with Java as a second or 18th language.
Admin
Man, I hate that operator.
Admin
Amen!
Admin
I don't know XML and am not that interested in it at the moment.
My main tool for getting files into our RDBMS is SQL Server DTS. Using delimited files is easier. When not using DTS I use looping and dynamic array(s) and or collections in VB. This way I don't have to hard code anything.
Admin
Google knows. You just have to know how to ask.
I searched for "MFC windows profile special folders".
There were at least 3 articles in the top ten (including the first), that would have lead you on the right path. They weren't perfect matches, but they at least mentioned SHGetSpecialFolderPath.
That was my fist try, better search terms might have given better results.
Admin
Surely you can't simply use Integer.parseInt because before it is divided by 100, the number is too large. 12 digits...
Admin
Now let's see all your examples rewritten to accommodate the case everyone missed...
captcha: bling. I win.
Admin
And how bad does your case of "not invented here" have to be that you write your own XML parser when there's a dozen to choose from for every remotely popular language?
Fixed-length fields are such an incredible pain in the ass to deal with for anything but the most primitively simple applications, that anyone suggesting their use for a new, non-trivial application should never be allowed to make any kind of design decisions.
Admin
My someone's full of themselves. Guess what? Sometimes the best tool for the job, is the best tool for the job. Imagine that.
Fixed-length is good because it's light weight, trivial to parse, and let's you skip records without reading and discarding them. If it's sorted you can even do binary searches etc.
On the downside forwards/backwards compatibility is a pain, and you have to be careful to make sure your lengths are sufficient.
But when it's the best fit, it's a powerful tool. Any "real developer" will know both the advantages and disadvantages of his tools, and how those apply to the problem at hand.
Sometimes the XML swiss army knife isn't the best tool for the job. Imagine that.
Admin
Why?
Using Perl's split function makes delimited quite straightforward.
Admin
Yes, but only if you're going to use all those twelve digits. Integer.parseInt has no problem with long numeric strings where the higher level digits are all zeros -- for example, Integer.parseInt("000000028007") will correctly yield (int) 28007. Then again, I guess we shouldn't take such risks -- if the space is there, it may well be used someday -- so it would be safer to use Long.parseLong(), which will easily handle 12-digit long integers.
There is, of course, a more generic way to deal with this problem, that would allow processing of arbitrary length strings as well as avoiding possible parser crashes: use regular expressions. With them, my earlier snippet could easily be rewritten as:
import java.util.regex.Pattern; import java.util.regex.Matcher;
String convert(String amount) { Pattern pattern = Pattern.compile("0*([0-9]+)([0-9]{2})");
}
Admin
Actually, no. Most examples seen here will work with this case, including mine.
Admin
I've got it! A "sarcasm quiz" required to post or register, replacing the captcha!
Admin
If you later want to do something more sophisticated than divide by 100, you'll want to use a different way that handles arbitrary-length strings: java.math.BigInteger or java.math.BigDecimal.
That's a WTF. If convert("400") should return "4", I doubt convert("4") should as well. You probably at least want to be throwing an exception if the regexp doesn't match...
Admin
Then again, maybe not. It all depends on what, exactly, that "something more sophisticated" is. For the task at hand - reformating a numeric string - regular expressions will do nicely.
Of course I didn't mean that code to be production ready... you'll notice my first snippet suffers from the same problem. I just meant to illustrate a non-WTF way to reformat a numeric string, without bothering much about error conditions.
Admin
So what you are saying is you didn't ask Google the right question?
Admin
Well, of course !c==0. c = 186282.2 miles per second.
So there.
captcha = "doom". Kind of what I felt when reading today's WTF
Admin
How in the world would someone have divined that question to ask? Ask it something sensible, like "obtaining My Documents in C++"
I asked it dozens of questions, none of which came up with anything remotely useful. It's not like I gave up after one search, I just couldn't divine the magic words necessary to make Google give me useful information. That's always a problem with indexing systems, from dictionaries to MSDN, you have to already know what you're looking for to find what you're looking for.Admin
How about '"my documents" programmatically' (Three words taken directly from your original post) Second hit led to a forum of someone asking that very question. Skimming through it, turns out the forum was a .net forum. But someone said "well here is how to do it the old way..."
You are right, you have to know how to ask the question. Thats true no matter who you are talking to (whether its an expert in the field, or an dumb index) Granted an expert in the field may figure out what you mean, and answer the question you really wanted to ask.
Admin
Ohhh so you read me spec? You KNOW that I will need hierarchical data? You KNOW the my 4 digit year field will need to be 4 digits (Yeah... Uhm I'll let someone else worry about the 10K issue)
I'll agree that the biggest problem with fixed width data is forward compatibilty. But that isn't to say the answer is to NEVER use them.
I love that some people think XML is the greates thing since the zero was invented. They fail to realize that its just a freaking file format.
Admin
Um how about...
amount = "000000028000" amount = amount.to_i * 0.01
Simpler is usually better.
Admin
Woops, forgot a to_s
amount = "000000028000" amount = (amount.to_i * 0.01).to_s
Admin
Anything that can't be captured in one line of Perl...can be done in one line of APL.
s/^0*([0-9]*)([0-9]{2})$/$1.$2/;
Admin
ahh yes. I forgot the mantra is XML, XML, XML
Turn that multimegabyte feeder into multigigabytes.
Nothing wrong with fixed length input. Try finding record (n) in an XML file, or even csv for that matter. At least in fixed length you can seek to (recordlength * n) in one statement.
Parsing fixed length isn't hard either.
But I guess this is modern times, and the simple solutions aren't enterprisey enough. Let's surround that flat data with bloated cruft.
Admin
One of the reasons they had fixed width fields was so that, it as easy to find records with just an index (i.e to find the nth record was a simple matter of seeking to n*record byte.
Anyway what's the problem with converting string to an integer(or double, if need to preform floating point) and the convert back to a string with the approperate format?
Admin
IMO these are such massive advantages that there need to be grave, specific reasons for me to use anything else for storing or transmitting data between differen systems.
Sure, there are cases where it's the wrong tool and it can be horribly abused. But I can think of very few cases where fixed-length fields would be a better alternative.
Admin
Well...
You pretty much always know what you are looking for when you are lookingf or something. To look for something without knowing what you are looking for, uhm? Do you do that often? Roam about looking for something, not knowing what you are looking for? Sounds like a mental disorder to me, perhaps altzheimers?
Admin
I tried, and "windows special folder path" was my first try. Top hit was http://support.microsoft.com/kb/194702 "How to locate Windows special folder locations".
Searching is easy, finding things can be hard. In this case, I showed how good I am at finding things/was lucky.
Admin
I'm always amazed at the number of people that will do a quick google search or, worse, join experts exchange, rather than actually know anything.
Admin
Covert string? No quack!
To help get rid of those mutant poodle puppies: CAPTCHA = ... uh, I'm a member so I don't get one :(:( can you kill them anyway?Admin
Don't forget that it takes 100 times as much memory and 100 times as much CPU to deal with XML as opposed to fixed width formats. If you are dealing with small payloads, XML is great. When you start dealing with larger payloads you start feeling pain very quickly. Try performing an XSLT transformation on a 600 MB XML file someday. It isn't pretty. I've been there, got the t-shirt, and never want to deal with it again.
Fixed format feeds have the singular advantage of being able to scale larger than delimited and much much much larger than XML. If you think that there is no place for fixed, you probably have not been working on domains that deal with large data problems.
Admin
But what if I take the rounding errors and transfer them to my own account?
Admin
Besides, there are quite lean XML parsers (stream parsers like SAX - there's even a stream-based variant of XSLT called STX) that don't have nearly as much overhead as you say. Admittedly they require mone manual work as well, but still scale much better to more complex data structures than fixed-length fields.
Admin
Java does let you turn a string into an int or a double, you just have to use the wrapper classes, Integer, or Double.
each has a constructor that takes in a String and converts it, then you can change it to a primitive data type with the intValue, or doubleValue methods.
of course this will change the string "000000028000" to 28000, (the constructors ignore leading zeros.) you can modify the number after you get its respective Integer, or Double, depending on what the string is like (whatever your rules are.).
Admin
Of course, there's no way that ratdump could possibly be being sarcastic himself..
Admin
Ooooouch - that gave me a headache.
thanks a lot...
Admin
No shit?!
Couldn't resist. Yes, this is wholly superfluous. But I needed to make it personal. jtl needs to learn. Fast!