Comment On The Longest Way to Zero

It was a pretty standard scenario: Lindsay F took over a small project from another department, and that project had a lot of “fun” surprises. Despite accomplishing a fairly simple task – reading in a fixed-width datafile, cleaning up a few things, and inserting it into a database – the application managed to be incredibly complex. Personaly, I blame the language they used: Java. [expand full text]
« PrevPage 1 | Page 2 | Page 3Next »

Re: The Longest Way to Zero

2007-03-30 09:19 • by mav (unregistered)
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.

Re: The Longest Way to Zero

2007-03-30 09:21 • by MET
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?

Re: The Longest Way to Zero

2007-03-30 09:26 • by Hit (unregistered)
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."

Re: The Longest Way to Zero

2007-03-30 09:26 • by ratdump (unregistered)
"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...

Re: The Longest Way to Zero

2007-03-30 09:29 • by Keith Hackney (unregistered)
129463 in reply to 129462
ratdump:
"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...


Noooo, really? Are you sure?

Captcha = Badgerknackers

Re: The Longest Way to Zero

2007-03-30 09:31 • by sootzoo
129464 in reply to 129461
Hit:
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."


ratdump:
"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...


Time elapsed: < 1 minute...

Re: The Longest Way to Zero

2007-03-30 09:31 • by KnobRotter (unregistered)
129465 in reply to 129461
Hit:
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."


But it doesn't. That's The Real WTF™

captcha = slashbottybasher

Re: The Longest Way to Zero

2007-03-30 09:36 • by Taki (unregistered)
Just for fun:

double result = Double.parseDouble( amount ) / 10;

Re: The Longest Way to Zero

2007-03-30 09:36 • by KnobRotter (unregistered)
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 =-).

Re: The Longest Way to Zero

2007-03-30 09:36 • by jtl (unregistered)
float f = Float.parseFloat("123.4");

Re: The Longest Way to Zero

2007-03-30 09:38 • by AdT (unregistered)
129469 in reply to 129461
Hit:
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."


Yeah, right, next thing you'll be claiming is that Java has a string replacement function and a date type... ridiculous.

Re: The Longest Way to Zero

2007-03-30 09:44 • by seenitbefore (unregistered)
Hasn't this article been posted like a million times? Except this time there was the always funny sarcasm ambush..

(THAT'S NOT FUNNY!)

Re: The Longest Way to Zero

2007-03-30 09:53 • by NameNotFoundException (unregistered)
Personaly I blame the invauable programmer who attempted to covert it this way, not the language.

SCNR.

Re: The Longest Way to Zero

2007-03-30 09:56 • by dkf (unregistered)
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.

Re: The Longest Way to Zero

2007-03-30 10:01 • by Stater of the Obvious (unregistered)
129476 in reply to 129467
"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.

Re: The Longest Way to Zero

2007-03-30 10:05 • by foobarbaz (unregistered)
129477 in reply to 129476
"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.

Re: The Longest Way to Zero

2007-03-30 10:12 • by I'm With Stupid (unregistered)
129478 in reply to 129476
Stater of the Obvious:
"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.



This post is why you should always use the quote button if you are quoting someone...

Re: The Longest Way to Zero

2007-03-30 10:13 • by Chris (unregistered)
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.

Re: The Longest Way to Zero

2007-03-30 10:15 • by Why do they come to me to die (unregistered)
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

Re: The Longest Way to Zero

2007-03-30 10:15 • by Chris (unregistered)
129483 in reply to 129480
Sorry-- I mean to peck out: !c == '0'

Re: The Longest Way to Zero

2007-03-30 10:16 • by dbs (unregistered)
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

Re: The Longest Way to Zero

2007-03-30 10:17 • by evanm
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"

Re: The Longest Way to Zero

2007-03-30 10:18 • by DOA (unregistered)
129487 in reply to 129464
sootzoo:
Hit:
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."


ratdump:
"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...


Time elapsed: < 1 minute...


That was funnier that the article itself

Re: The Longest Way to Zero

2007-03-30 10:19 • by I hate you all so very, very much (unregistered)
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.

Re: The Longest Way to Zero

2007-03-30 10:21 • by Helio (unregistered)
129489 in reply to 129475
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?

Re: The Longest Way to Zero

2007-03-30 10:25 • by Jeltz (unregistered)
129490 in reply to 129487
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?).

Re: The Longest Way to Zero

2007-03-30 10:43 • by erKURITA (unregistered)
That really made me say "WTF?"

Re: The Longest Way to Zero

2007-03-30 10:48 • by JamesCurran
129494 in reply to 129466
Taki:
double result = Double.parseDouble( amount ) / 10;


Ahem....

double result = Double.parseDouble( amount ) / 100;

Yes, you've committed the classic meta-WTF of proposing a better way --- and made matters worse!

Re: The Longest Way to Zero

2007-03-30 10:49 • by PoodlePuppyHater (unregistered)
129495 in reply to 129492
You stood astonished and said "Worse than failure?"

BTW: I hate dogs... Captcha: alarm :-)

Re: The Longest Way to Zero

2007-03-30 10:50 • by Anonymous Coward (unregistered)
129496 in reply to 129488
I hate you all so very, very much:
captcha: NEXT MORON THAT TELLS ME WHAT THEIR CAPTCHA WAS I SLAUGHTER 10,000 POODLE PUPPIES.


Whoah, calm down, man!


captcha: waffles! :D

Re: The Longest Way to Zero

2007-03-30 10:57 • by Diego (unregistered)
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

Re: The Longest Way to Zero

2007-03-30 11:00 • by Coolvibe (unregistered)
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)

Re: The Longest Way to Zero

2007-03-30 11:03 • by bob the dingo
129501 in reply to 129467
KnobRotter:
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;
}


Haha, that's great.

And because it's a string, it can hold its dossiers without the need for a briefcase!

Re: The Longest Way to Zero

2007-03-30 11:03 • by Coolvibe (unregistered)
129502 in reply to 129497
Diego:

remember: never use float and double for currency data

Diego
HP


Wise words...

captcha: cognac. Bleh

Re: The Longest Way to Zero

2007-03-30 11:22 • by andy brons (unregistered)
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.

Re: The Longest Way to Zero

2007-03-30 11:25 • by Derrick Pallas
129516 in reply to 129461
Hit:
Because no matter how many of these articles people will read on this site, they STILL will not get the concept of "Sarcasm."


OMFG WTF no wey u id10t r0fL!

Re: The Longest Way to Zero

2007-03-30 11:33 • by Not A Robot (unregistered)
129521 in reply to 129461
JAVA DOES HAVE A FUNCTION TO CONVERT A STRING TO A NUMBER YOU IDIOT!

Re: The Longest Way to Zero

2007-03-30 11:49 • by CodingForPretend (unregistered)
129529 in reply to 129495
[quote user="PoodlePuppyHater"]You stood astonished and said "Worse than failure?"[quote]

That's just awesome!

Re: The Longest Way to Zero

2007-03-30 12:01 • by Jno (unregistered)
129532 in reply to 129513
andy brons:
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.


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.

Re: The Longest Way to Zero

2007-03-30 12:03 • by dolo54 (unregistered)
129534 in reply to 129521
Not A Robot:
JAVA DOES HAVE A FUNCTION TO CONVERT A STRING TO A NUMBER YOU IDIOT!


Yeeeeees yeeeeeees feel the anger surging inside you. Give in to it... thee dark side is stronger!

Re: The Longest Way to Zero

2007-03-30 12:16 • by andy brons (unregistered)
129539 in reply to 129532
Jno:
andy brons:
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.


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.

Yes, I know that in a land far away and a long time ago this was the way. I am working with relatively recent software and still have to deal with them. It's ridiculous.

Re: The Longest Way to Zero

2007-03-30 12:23 • by Pap
129543 in reply to 129464
sootzoo:
Hit:
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."


ratdump:
"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...


Time elapsed: < 1 minute...


I'd like for Alex to give us a ruling on this one, complete with timestamps to the second.

I feel your pain

2007-03-30 12:27 • by who? (unregistered)
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.

Re: The Longest Way to Zero

2007-03-30 12:40 • by Old Timer (unregistered)
129552 in reply to 129513
andy brons:
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.


Fixed length data files are a lot easier to parse than delimmeted file or (yuch!) xml.

Re: The Longest Way to Zero

2007-03-30 12:46 • by scott2718281828 (unregistered)
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.

Re: The Longest Way to Zero

2007-03-30 12:49 • by Henry McBrevity (unregistered)
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]

Re: The Longest Way to Zero

2007-03-30 13:01 • by sjs (unregistered)
amount = "000000028000"
amount = sprintf("%0.2f", amount.to_f / 100)

(ruby)

Re: The Longest Way to Zero

2007-03-30 13:11 • by Malfist (unregistered)
Why has noone said anything about the break; at the end of the for loop? What uses is it? I couldn't find any!

Good thing he wasn't using C

2007-03-30 13:13 • by C programmer (unregistered)
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.

Re: The Longest Way to Zero

2007-03-30 13:16 • by strcmp (unregistered)
Inserting the dot first simplifies things (and you don't lose precision by converting to anything). In perl:

substr($_, -2, 0) = '.';
s/^0+//;
« PrevPage 1 | Page 2 | Page 3Next »

Add Comment