• 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.

  • (cs)

    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?

  • 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."

  • 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...

  • Keith Hackney (unregistered) in reply to ratdump
    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

  • (cs) in reply to Hit
    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...

  • KnobRotter (unregistered) in reply to Hit
    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

  • Taki (unregistered)

    Just for fun:

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

  • 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 =-).

  • jtl (unregistered)

    float f = Float.parseFloat("123.4");

  • AdT (unregistered) in reply to Hit
    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.

  • 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!)

  • NameNotFoundException (unregistered)

    Personaly I blame the invauable programmer who attempted to covert it this way, not the language.

    SCNR.

  • 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.

  • Stater of the Obvious (unregistered) in reply to KnobRotter

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

  • foobarbaz (unregistered) in reply to Stater of the Obvious

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

  • I'm With Stupid (unregistered) in reply to Stater of the Obvious
    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...

  • 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.

  • 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

  • Chris (unregistered) in reply to Chris

    Sorry-- I mean to peck out: !c == '0'

  • dbs (unregistered)
    Comment held for moderation.
  • (cs)

    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"

  • DOA (unregistered) in reply to sootzoo
    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

  • 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.

  • Helio (unregistered) in reply to dkf

    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?

  • Jeltz (unregistered) in reply to DOA

    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?).

  • erKURITA (unregistered)

    That really made me say "WTF?"

  • (cs) in reply to Taki
    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!

  • PoodlePuppyHater (unregistered) in reply to erKURITA

    You stood astonished and said "Worse than failure?"

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

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

    Whoah, calm down, man!

    captcha: waffles! :D

  • 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

  • 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)

  • (cs) in reply to KnobRotter
    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!

  • Coolvibe (unregistered) in reply to Diego
    Diego:
    remember: never use float and double for currency data

    Diego HP

    Wise words...

    captcha: cognac. Bleh

  • andy brons (unregistered)
    Comment held for moderation.
  • (cs) in reply to Hit
    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!

  • Not A Robot (unregistered) in reply to Hit

    JAVA DOES HAVE A FUNCTION TO CONVERT A STRING TO A NUMBER YOU IDIOT!

  • CodingForPretend (unregistered) in reply to PoodlePuppyHater

    [quote user="PoodlePuppyHater"]You stood astonished and said "Worse than failure?"[quote]

    That's just awesome!

  • Jno (unregistered) in reply to andy brons
    Comment held for moderation.
  • dolo54 (unregistered) in reply to Not A Robot
    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!

  • andy brons (unregistered) in reply to Jno
    Comment held for moderation.
  • (cs) in reply to sootzoo
    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.

  • 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.

  • Old Timer (unregistered) in reply to andy brons
    Comment held for moderation.
  • 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.

  • Henry McBrevity (unregistered)
    Comment held for moderation.
  • sjs (unregistered)

    amount = "000000028000" amount = sprintf("%0.2f", amount.to_f / 100)

    (ruby)

  • 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!

  • 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.

  • 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+//;

Leave a comment on “The Longest Way to Zero”

Log In or post as a guest

Replying to comment #:

« Return to Article