- 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
Now THAT is ugly. Wow.
I'm always amazed at what people will churn out rather than doing a quick google search to find the right answer. Thank god google knows everything.
Admin
I cannot see where in the code the value of c gets added to anything in order to actually compute the number. Am I missing something? Or is that just another facet of this interesting code?
Admin
Oh boy. Get ready for the 100s of comments saying inane crap like "JAVA DOES HAVE A FUNCTION TO CONVERT A STRING TO A NUMBER YOU IDIOT".
Because no matter how many of these articles people will read on this site, they STILL will not get the concept of "Sarcasm."
Admin
"You’d think that the developers of Java would provide a simple way to covert a string, say "000000028000", into a number, say 280.00."
Uhh there is...
Admin
Noooo, really? Are you sure?
Captcha = Badgerknackers
Admin
Time elapsed: < 1 minute...
Admin
But it doesn't. That's The Real WTF™
captcha = slashbottybasher
Admin
Just for fun:
double result = Double.parseDouble( amount ) / 10;
Admin
I don't know of any language that allows you to 'covert' a string.
I may write my own function though. Something like:
protected string covertString(string inStr) { string outStr = inStr; outStr.Attributes.Add(Sunglasses.Dark); outStr.Attributes.Add(Accent.Scottish); outStr.Attributes.Add(Clothes.Camo); return outStr; }
Captcha = TakeThat
Editor's Note: Fixed Typo. Very clever =-).
Admin
float f = Float.parseFloat("123.4");
Admin
Yeah, right, next thing you'll be claiming is that Java has a string replacement function and a date type... ridiculous.
Admin
Hasn't this article been posted like a million times? Except this time there was the always funny sarcasm ambush..
(THAT'S NOT FUNNY!)
Admin
Personaly I blame the invauable programmer who attempted to covert it this way, not the language.
SCNR.
Admin
The Real WTF! is that nobody (Alex included) seems to have noticed that this is a string-to-string mapping.
Ugly though. And unlikely to deal with fractional currencies either.
Admin
"I don't know of any language that allows you to 'convert' a string".
Many (Most? All?) scripting languages do. Tcl, ferinstance, will gladly do math operations on a string that looks like a number.
Admin
"Many (Most? All?) scripting languages do. Tcl, ferinstance, will gladly do math operations on a string that looks like a number."
I think you missed the thrust of his post entirely.
Admin
This post is why you should always use the quote button if you are quoting someone...
Admin
String.valueOf(c).equals("0") is pretty inane. Why not just: c == '0' ?
On another note, the string is being checked for null and empty conditions. At least it won't stupidly blow up.
Admin
To be fair. Some times string to number converters spot things like a strig that starts with zero and only has zeros and ones in it and thinks that it is a binary number. Perhaps the person who wrote the code originally wrote it as a really bad way to get aroung this problem then converted it to Java without having a clue about Java.
Or the moron was just bored and got paid ploc
Admin
Sorry-- I mean to peck out: !c == '0'
Admin
There's an element of truth in stuff like this. I've been running up in Java for the last couple years now, and there is a real challenge sometimes wading through the thousands of API pages trying to find a method that does what you want to do.
Now, I'm not forgiving this egregious twisting of the langauge - because base type conversion is... well... pretty base. There's a zillion quicky reference sheets for this sort of thing, but even a Java 101 programmer should have known about String to Float methods.
Going OOP is hard for folks. If it's not in the base language spec, I guess ya gotta code it from hand.
http://planet-geek.com
Admin
Now, I haven't coded in Java in a long time, but doesn't this function go into an infinite loop, adding an infinite number of decimal points, if the inputted string ends with "00"
Admin
That was funnier that the article itself
Admin
captcha: NEXT MORON THAT TELLS ME WHAT THEIR CAPTCHA WAS I SLAUGHTER 10,000 POODLE PUPPIES.
..
captcha: ninjas ... which is Apropó because that's exactly what I'm going to send to your house the next time you tell me what your captcha was.
Admin
To be fair, this is a slightly tricky example, in that you have to care about several special cases (null string, empty string, string with spaces, (non-)zero fractional part, etc). Still, you got to wander how many rocket scientists are needed to get to something like this:
String convert(String amount) { if (amount == null) return amount;
String trimmed = amount.trim(); if ("".equals(trimmed)) return amount;
int value = Integer.parseInt(trimmed); int integer = value / 100; int fraction = value % 100; return integer + "." + (fraction < 9 ? "0" : "") + fraction; }
Granted, still no pinnacle of perfection, but a lot better, don't you think?
Admin
Why do people suggest Double.parseDouble() when this most likely would give incorrect results? The comments are indeed often a greater WTF than the articles.
The guy seem to want to handle decimal floats, not binary ones. I bet he didn't know of the class for storing decimal numbers (Decimal?).
Admin
That really made me say "WTF?"
Admin
Ahem....
double result = Double.parseDouble( amount ) / 100;
Yes, you've committed the classic meta-WTF of proposing a better way --- and made matters worse!
Admin
You stood astonished and said "Worse than failure?"
BTW: I hate dogs... Captcha: alarm :-)
Admin
Whoah, calm down, man!
captcha: waffles! :D
Admin
fuah! of course that string comes from cobol :)
and the old smart ones never want to convert it to a f'ing double.
why go to float point? (perfect to ruin your currency system) you already have the data in a fixed point nicely BCD encoded
and for the OOP ones, you only have to encapsulate it in a class and do the math manually. fixed point is a piece of cake
remember: never use float and double for currency data
Diego HP
Admin
In C there's atol and friends, which are actually aliases for strtol and strtod in most operating systems. They will eliminate leading zeros for you.
C pwnage :)
(captcha: yummy... indeed. Let's go for another cup of java)
Admin
And because it's a string, it can hold its dossiers without the need for a briefcase!
Admin
Wise words...
captcha: cognac. Bleh
Admin
The biggest WTF is fixed length data files. Who writes these things? They are the biggest pain in @ss. I just don't understood the reasoning for them.
Admin
OMFG WTF no wey u id10t r0fL!
Admin
JAVA DOES HAVE A FUNCTION TO CONVERT A STRING TO A NUMBER YOU IDIOT!
Admin
[quote user="PoodlePuppyHater"]You stood astonished and said "Worse than failure?"[quote]
That's just awesome!
Admin
Too young to remember COBOL and PIC statements, then? Once upon a time, when unicorns still roamed free, you had to make your data look like it came on a Hollerith card. I'm still working with a system which gets multimegabyte feeder files in a fixed width format and those files get fed to a COBOL suite.
Admin
Yeeeeees yeeeeeees feel the anger surging inside you. Give in to it... thee dark side is stronger!
Admin
Admin
I'd like for Alex to give us a ruling on this one, complete with timestamps to the second.
Admin
Ha! I've seen nearly identical code in the project I am currently working on. Scary.
The guy responsible for it here also has a nifty date handling function that is at least as bad. To make matters worse, he doesn't get the concept of re-usable code. I did a quick check one day and found 123 instances of his crappy date function in 1 package.
(edited only to change the prompt) bash# grep "public static String revText(" *.java | wc -l 123
Yes friends and neighbors, you read that correctly. It accepts dates (as a string) returns a date (as a string in a new format), is poorly named, is buggy as hell, is public static and there are 123 instances of it in 1 directory. Apparently, it is easier to copy this buggy mess into every file he touched than to just call one of the existing 122 instances in that package.
Some people seriously need to be hounded out of IT completely.
Admin
Fixed length data files are a lot easier to parse than delimmeted file or (yuch!) xml.
Admin
If someone wants to send you a file with fixed-length fields, you probably should take it because they may not know how to do anything else.
I have gotten comma-delimited files with commas in the data fields. I have gotten quote-qualified data files with unescaped quotes in the data fields. Some people don't see any problem with tabs, pipes or carriage returns in their data fields. And I've usually gotten blank uncomprehending stares when trying to explain why these are problems.
At one job we got a weekly data file from a huge corporate big-iron COBOL shop. It wasn't hard to deal with, but we had one anal manager who insisted that they change it to comma-delimited quote-qualified. It took over six months to make the change. I assume most of the time was spent bringing a COBOL programmer back from the dead, or hatching a new one in some diabolical secret lab. When the long-awaited day came that the first file arrived in the new format, to no one's surprise they had just added "," literals between every fixed-length field, making the file that much bigger and forcing me to trim all the fields on import.
Admin
http://java.sun.com/j2se/1.5.0/docs/api/java/math/BigDecimal.html#BigDecimal(java.lang.String)
Examples: The value of the returned BigDecimal is equal to significand × 10 exponent. For each string on the left, the resulting representation [BigInteger, scale] is shown on the right.
"0" [0,0] "0.00" [0,2] "123" [123,0] "-123" [-123,0] "1.23E3" [123,-1] "1.23E+3" [123,-1] "12.3E+7" [123,-6] "12.0" [120,1] "12.3" [123,1] "0.00123" [123,5] "-1.23E-12" [-123,14] "1234.5E-4" [12345,5] "0E+7" [0,-7] "-0" [0,0]
Admin
amount = "000000028000" amount = sprintf("%0.2f", amount.to_f / 100)
(ruby)
Admin
Why has noone said anything about the break; at the end of the for loop? What uses is it? I couldn't find any!
Admin
Maybe it was a (bad) C programmer who remembered that C library functions for number conversions interpret numbers starting with zero as being octal and therefore unable to parse 0000028000 correctly.
Admin
Inserting the dot first simplifies things (and you don't lose precision by converting to anything). In perl:
substr($_, -2, 0) = '.'; s/^0+//;