• AP (unregistered)

    Frist!

  • GW (unregistered)

    Do some people not even have access to google? or a book? or a brain? or a colleague? Or even simply intellisense and some vague sense of curiosity....

  • (nodebb)

    But everyone knows that floating point operations lead to a loss of precision, so the only way to correctly do math with floating point numbers is to convert them to strings.

  • G (unregistered)

    Bonus points also for not specifying culture in float.ToString() '.' might be a decimal point, thousands separator or not used at all depending on local settings.

  • Jens (unregistered)

    The code doesn't even work. It truncates to two digits after the separator, then checks if the second digit is 9. If it isn't, nothing more happens. So if the input is 1.455, it will return 1.45. If the second digit IS 9, it will round by increasing the first digit. So 1.49 becomes 1.5.

    Then there is the bug. The code tries to test for the edge case x.99, in which case it intends to increase the integer part by one and return 0 decimals. I.e. (x + 1).0. However, it tests for <= 9, not < 9, so it fails to detect this and will round x.99 to x.10.

  • Jonathan (unregistered)

    This looks like C#. For those wondering on a good way to get a rounded string in C#, consider: float myFloat = 32454367.45555555F; string roundedTo1DecimalPoint = myFloat.ToString("#.#", System.Globalization.CultureInfo.InvariantCulture); string roundedTo2DecimalPoint = myFloat.ToString("#.##", System.Globalization.CultureInfo.InvariantCulture); string roundedTo3DecimalPoint = myFloat.ToString("#.###", System.Globalization.CultureInfo.InvariantCulture);

  • Felix (unregistered)

    or good old myFloat .ToString("n2")

  • Matt Westwood (unregistered)

    I've just had to reject a module because it uses if ... elseif ... elseif ... ... else ... endif instead of doing what ought to have been done: configure the data as a map and do a key-value lookup. All very well, but the engineer in question was told to change it 18 months ago and repeatedly since. In the end I asked him straight up: "Do you understand the concept of a map?" and when pressed, he had to answer "... no."

    The fail is great with this one.

  • yuki.n (unregistered)

    Also knows as Stringly Typed. https://blog.codinghorror.com/content/images/uploads/2012/07/6a0120a85dcdae970b0177437c9f53970d-800wi.jpg

  • Andrew (unregistered)

    I have a feeling that this would epicly fail on extremely big or extremely small values. You know, the ones involving "e".

  • ChrisEvansForBakeOff (unregistered) in reply to Matt Westwood

    That ain't no engineer.

    So what you're saying is, you failed to provide adequate training? ;)

  • RichP (unregistered)

    King Philip came over for good soup. Kingdom, Phylum, Class, Order, Family, Genus, Species.

    Yippie! I still remember something from Jr. High science. Wait, it's obsolete information now? (cue sad trombone sound).

  • Your Name (unregistered)

    We should suppose that this code does just what it does on purpose. Customers want to have it just this way! So, of course the programmers had to eschew the builtin functions. There just aren't any that round numbers in just the strange way required. So they had to concoct their own. :-P

  • Practical Dev (unregistered) in reply to Matt Westwood

    Aren't you the smug elitist, pressing for a specific solution (Map) where the chosen solution fits the functionality just fine? (I don't know, of course, whether it does: you failed to specify.)

    I found at least 1 use case in which using a Map is bad practice: when the data you're storing should get exposed neither in the source code nor via reflection.

    I was developing a shared library that would store database credentials, use them internally to set up a connection, without ever exposing them itself. I tried various solutions, including Maps, but they'd all expose the credentials either in source code or via reflection.

    The only way I found that didn't, was by using a simple if-else-if-else construction inside a method, and storing the data in a char array.

    I'm appalled that this was necessary, for I too am an elitist and would rather have used various Map implementations. But practicality forced my hands.

    Back to you. Do you enjoy micro-managing your coworkers?

  • Carl Witthoft (google)

    "Does this string code work?"

    " 'Frayed knot"

    Woo hoo got that inB4 y'all

  • guest (unregistered) in reply to Practical Dev

    That's not micromanaging. That's just code review. Micromanaging would be if the engineer in question came back with a good reason for using an if-else if-else instead of a Map and you told him to use the Map anyway.

    An engineer who comes back and says that he doesn't know what a Map actually is is someone who should be thinking about all of those data structures classes they skipped out on in school, thinking that the stuff in that course couldn't possibly ever be relevant to the real world...

  • (nodebb) in reply to Practical Dev

    I didn't try it (partly because your post left out details about your environment), but I have a hard time to believe that your solution with an array would prevent getting the data using reflection. Besides if you develop a shared library I would expect any configuration of your database connection to be outside of your library, otherwise it would be coupled to another system, which is not the property of a well designed shared library.

  • Sam Judson (github)

    ShortenFloatString(1.999f) gives "1.10" - which is SOOOO wrong. ShortenFloatString(1.091f) gives "1.1" as (with no trailing zero), which is also wrong, but a bit closer. ShortenFloatString(1.089f) gives "1.08" which is also wrong and weird.

    A real WTF here...

  • SolePurposeOfVisit (unregistered)

    The only thing wrong with this superlative bit of code is that it returns the wrong string. May I humbly suggest "Brillant"?

  • Piece of String (unregistered) in reply to Carl Witthoft

    Best. Joke. EVAR.

    Not this one of course. The one who's punch line is referenced.

  • ATF (unregistered) in reply to Matt Westwood

    Or perhaps with you, your colleagues and the manager for waiting 18 months to help a co-worker out? Nah.. that couldn't be a thing right?

    Seriously, if you waited 18 months to ask someone if they knew what a map was and they said "no", I'd fire both of you.

  • (nodebb)

    To be fair, I've never come across a language that supported a rounding function that allowed you to round x.yzw to x.yz if z is not 9, and to x.(y+1) if z is 9. (And possibly the rule that x.99n should be rounded to x.10, though that probably is just a bug.)

    @Sam Judson: 1.091 being shortened to 1.1 isn't actually wrong; it's pretty clear from the final couple of blocks that the intent is actually to shorten it to one decimal place.

    If the "num <= 9" bug was corrected, and an else clause was added for the "if(xString2[2] == '9')" block that stripped off the final character of xString2, it would actually work -- provided your rounding rule was "9 rounds up, everything else rounds down".

    Of course, even with those fixes it would still be a WTF, but it would be a correctly functioning one if you had that rather odd requirement. And it would be only one bugfix away from rounding more or less normally! (In effect, not manner.)

    Addendum 2016-10-04 04:31: It would be fun to see what the original coder would come up with if they were told that a different field had to be rounded to two decimals instead of one.

    I mean, I wouldn't want to have it in my codebase, but it'd be fun to point at and laugh.

  • (nodebb) in reply to guest

    An engineer who comes back and says that he doesn't know what a Map actually is

    … is someone who is only fit for hitting code with a hammer, not actually an engineer. They should know. Or they should deal with their ignorance the moment they realise they're ignorant. But actually… if you don't know Lists and Maps, you're a crappy code monkey, not an engineer; it takes dedication to stupidity to make it as a programmer without learning the basic tools of the trade.

  • (nodebb) in reply to GW

    Two minutes of thinking are way harder than 30 minutes of typing, you know.

  • Bob (unregistered)

    so this is how you implement Math.Round(double value, int digits)!

  • Derek (unregistered) in reply to Jonathan

    "Looks like C#' - Which bit of 'Which brings us to today’s C# code, from Aaron.' gave that away?

  • Norman Diamond (unregistered)

    To be fair, I've never come across a language that supported a rounding function that allowed you to round x.yzw to x.yz if z is not 9, and to x.(y+1) if z is 9.

    Me neither. That's why, if I were given such a requirement, I would roll my own instead of looking for a library with it.

    I bet some sales tax jurisdiction has a rule somewhat like that.

    (And possibly the rule that x.99n should be rounded to x.10, though that probably is just a bug.)

    Yes, that looks like a WTF. Front page material.

  • Norman Diamond (unregistered)

    Two minutes of thinking are way harder than 30 minutes of typing, you know.

    The "Programming is Hard" thread is ------------------->

  • Anon (unregistered)

    I particularly loved the line;

    int num = int.Parse(xString2[1].ToString())

    All strings in C# are taken as char arrays. So "xString2[1]" returns the char-typed value, hence the need to call ToString() in "xString2[1].ToString() so that it could be parsed by "int.Parse()". Alternatively (if you really had to)...;

    int num = (int)xString[1];

  • Andreas (google) in reply to G

    It's a dot. If you're using anything else, "You’re Doing It Wrong™".

    OR: It's working on my machine. Clearly, your machine is broken.

Leave a comment on “Not the Shortest Shortener”

Log In or post as a guest

Replying to comment #:

« Return to Article