• G Dawg (unregistered)

    This code works. He probably had to get it done quickly and this was the best way to do it. I dont really see the WTF here. Granted its not pretty and probably could use a little cleanup, but show me code that doesn't?

  • Ateisti (unregistered)

    I'm speechless.

  • Anon E. Mouse (unregistered)

    Round(CDbl(theString), precision)

  • Steve (unregistered)

    wow, can't believe someone defended this code

  • Hexy (unregistered)

    Yes, I'm sure it was WAY faster to write this than to use something like what Anon E. Mouse suggested.

  • Jeff S (unregistered)

    The biggest WTF in history, perhaps, for two reasons:

    a) the code itself
    b) someone DEFENDED THIS CODE !!!!!!

    both are simply unbelievable. This must be staged. This stuff can't exist in the real world, can it? Is this like a "hidden camera" type site? These are actors, right? like wrestling, kind of?

  • Razzie (unregistered)

    I'm not even going to read this crap :)

  • Zoomer (unregistered)

    Be careful with labeling this a WTF!


    Open VB 6 and go to the immediate window. Enter:

    ?Round(1.5), Round(2.5)
    2 2

    WTF!?!? That's not the way I learned to round numbers! Banker's rounding? WTF?!?!!

    Poor SOB may have been trying to side step VB's quirks. Admittedly, there are much simplier ways to do this...

    See KB 196652
    http://support.microsoft.com/default.aspx?scid=kb;en-us;196652

  • Jeff S (unregistered)

    This function, as written, while still a WTF, does do more than just round: it handles formatting by adding trailing zeroes to a decimal if needed, and it allows for rounding in 1000s or 100s if you pass a negative value for the # of decimal places.

    So, it does do more than using the built-in function Round() would do.


    Maybe it DOES require 100 lines of code to handle this, manipulating the string character by character?

    Nah!

    Something like this works just fine, too:

    Function RoundIt(f As Double, DecPlaces As Integer) As String
    If DecPlaces <= 0 Then
    f = Int(f * (10 ^ DecPlaces) + 0.5)
    f = f / (10 ^ DecPlaces)
    RoundIt = f
    Else
    RoundIt = Format(f, "#." & Left("00000000000", DecPlaces))
    End If
    End Function

  • James (unregistered)

    further proof that vb coders get paid per line...

  • Jeff S (unregistered)

    >>further proof that vb coders get paid per line...

    This WTF, like many others here, has absolutely NOTHING to do with VB.


    It has to do with the algorithm devised by the programmer to accomplish their goal (which is language-independant) as well as the physical implementation of that algorithm, which could be equally ugly (or even moreso) in C++ or Java or whatever your language of choice is.

  • Guayo (unregistered)

    I can't believe the coincidence... I'm optimizing some java code right now and there is a ugly function that builds a lot of strings to round a string representation of a number. This function is called compulsively (and a lot of times unnecessary) in a big complex loop. The code base I'm working at it's ugly with lot of business code in Swing events.
    Because of time constraints this code needs to be refactored (but the task is titanic) instead of just thrown away. The sad part is that odd behaviour in refactored functions needs to be reproduced as a lot of code depends of such side effects and odd behaviours. The team that build this abomination had little java background but I'm pretty sure that was compensated with a lot of "vb experience".
    I'm not allowed to show this code.

  • Robert (unregistered)

    Jeff,
    Granted, this would be ugly in Java or C++ to written like this. Isn't it odd though how these kinds of 'algorithms' tend to pop up rather often in VB?

  • Hassan Voyeau (unregistered)

    Adding some clarity to Roberts comment :

    Isn't it odd though how these kinds of 'algorithms' tend to pop up rather often [on the WTFs on this site].

    There are a lot of factors to be considered before we can vituperate VB.

  • fluffy (unregistered)

    floor(num + 0.5) (or whatever is equivalent in your language which isn't C) also works.

  • fluffy (unregistered)

    Er, for just doing rounding to the nearest 1.

    Rounding to any arbitrary value:

    float RoundVal(float val, float precision)
    {
    return floor(val/precision + 0.5)*precision;
    }

  • ak (unregistered)

    Oh, yeah, your first code example floor(num + 0.5) was the very first thing I thought about when I saw the code. It was a programming exercise that we got during my first programming class, and we should search for a way to implement a round() function without using any if or something like that. Nobody managed to find the (simple) solution, and when the teacher told us the solution, everybody was kind of enlightened. :-)

  • Tim Cartwright (unregistered)

    Zoomer, read that article you posted more carefully. There is a very VALID reason for rounding the way they do.

    "Banker's Rounding
    When you add rounded values together, always rounding .5 in the same direction results in a bias that grows with the more numbers you add together. One way to minimize the bias is with banker's rounding.

    Banker's rounding rounds .5 up sometimes and down sometimes. The convention is to round to the nearest even number, so that both 1.5 and 2.5 round to 2, and 3.5 and 4.5 both round to 4. Banker's rounding is symmetric. "

  • thomas woelfer (unregistered)

    oh, the wonders of software engineering.

    WM_ROFL
    thomas woelfer

  • minime (unregistered)

    I thought programmers were usually lazy buggors ?!

  • Anon (unregistered)

    Robert,

    >> Isn't it odd though how these kinds of 'algorithms' tend to pop up rather often in VB?

    Given VB is the most common language for internal software development (if you disagree, feel free to cite stats) it makes perfect sense.

    Also, VB has the advantage of being 'human readable' - so people who know other languages can still usually read a VB WTF and understand it (and laugh!).

  • foxyshadis (unregistered)

    True. The worst of C/Perl wtfs are beyond ordinary mortal comprehension.

    However, this could win an award for "most difficult solution to an easy problem". I am in awe of the amount of time he must have spent debugging this.

  • Andrew (unregistered)

    "most common language for internal software development"

    Wouldn't that be COBOL with a landslide?

  • Jason (unregistered)

    Andrew -> I completely agree. Anon -> I think that Cobol has about 70%, so if you want to compare stats, go right ahead.

    Not to knock VB or anything, but I too came from VB in some form, or another, and its not that VB programmers are dumber (as hinted to by some of the comments), its that VB is easier to learn, so developers (or non-developers for that matter) can more easily screw it up, especially while they're learning. And if its a non-developer's code, then unfortunately, they usually don't have the ambition to go out and learn how to do it right. :-)

  • Zka (unregistered)

    ohno ohno ohno! :D

  • Anon (unregistered)

    Jason,

    >> I think that Cobol has about 70%, so if you want to compare stats, go right ahead.

    In terms of existing code base - I could believe that.

    But in terms of development? I would be surprised...

  • Ilpalazzo (unregistered)

    Has anyone actially tried this code? I am not sure this one really works correctly, since it does not seem to generate a carry to the next digit when a "9" becomes a "0" ...

  • LargePig (unregistered)

    I'm a VB developer, this is bloody stupid, but this is not unique to VB, you can be this dumb in any language!

  • VB Pig (unregistered)

    I'm impressed by the time-saving device of declaring char as string * 1.
    And then declaring "done" as a time-wasting variant.

    Sheer class.

  • Yakko (unregistered)

    Ummmm.... and there's absolutely no checking to see that the string that is being 'rounded' is numeric.

  • Defiler (unregistered)

    >> Ummmm.... and there's absolutely no checking to see that the string that is being 'rounded' is numeric.
    Well, since VB6 is a strictly typed language, like Haskell... oh, wait..

  • Prakash (unregistered)

    Why the fuck the font is all so small/tiny

  • Anonymous Coward (unregistered)

    >> Isn't it odd though how these kinds of 'algorithms' tend to pop up rather often in VB?

    Well, that’s like saying its odd that 6 legged dogs and purple chickens appear most often in Kindergartner art.

    >> Further proof that vb coders get paid per line...

    No, I'd be willing to bet they get paid per hour(and not very well)... most are not "Titled" Programmers. They’re just poor slobs who are required to write something because they can (or just can't say no) and their bosses are too cheap to hire a real coder. Wasteful? Yes. Blatant Fraud? No, we’ll reserve that for the pros.

    Yeah, I'll almost defend his code... at least the part where he had to re-invent the wheel here... not the part where he made it 100 lines.

    I did some expirements in VB... the round() function (as well as math.round() in .net) uses Bankers Rounding... I've used it for years, and never known. The whole round(x +.5) thing has thrown me off a few times too, but I worked around it. (not quite that far around...)

    I found it odd though... that VB uses Bankers Rounding but Excel (a program for armature accountants) uses Arithmetic Rounding (theres a WTF):
    X Excel VB
    0.5 1 0
    1.5 2 2
    2.5 3 2
    3.5 4 4
    4.5 5 4
    5.5 6 6

    Heres Microsoft’s expanation: http://support.microsoft.com/default.aspx?scid=kb;EN-US;196652

    When a company cranks out a shoddy product (or internal process) because there too cheap to find a real programmer or get someone some training; I don’t think WTF (though I’ll still laugh). But when a company that should know better, didn’t think that a Programming Language should include a round function for people other than in the finance industry, that’s a WTF.

  • Chris (unregistered)

    Here's another funny thing MS has to say with regard to rounding...

    "If you want to write a rounding function that will round decimal values according to predictable rules, you should write your own."

    From here:
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/modcore/html/deconconversionroundingtruncation.asp

    I guess this guy took that to heart...

  • Jeff S (unregistered)

    Guys/Gals -- just because you don't trust a built-in function and decide you write your own does NOT mean you need to come up with "algorithms" like this or write code as sloppy as this. End of debate on that topic. Next!

  • Jeremy Morton (unregistered)

    Just because it's called "Banker's Rounding" doesn't mean it's only used in the finance industry. The exact same rounding method is used when determining significant digits when measuring scientifically. I've always considered the "normal" arithmetical round to be broken.

  • MIles Archer (unregistered)

    armature accountants? Are you winding me up?

  • Rainer (unregistered)

    Hopefully no banking software uses floating point math: floor(num+0.5) with num=2.4 might give 1.9999999999999, following operations increase the error. Fixed-point arithmetic is a must for banking.

    Nevertheless there might be a better way than using strings for that - although you can get arbitraty presicion that way, like RoundString(num, 1000) ...

  • peter beaguely (unregistered)

    shit this guy is hard core. i can only hope to develop programming skills like this.

  • Fleep (unregistered)

    "its not that VB programmers are dumber ... its that VB is easier to learn, so developers ... can more easily screw it up, especially while they're learning."

    My main gripe with other PHP programmers. I came from C for desktop app development and went to PHP for web app development. The atrocities I have seen (but unfortunately, haven't documented) would make you weep.

  • Salk (unregistered) in reply to Jeff S

    This code doesnt work good.. for example if u round 1.3445  with 2 decimals u would get 1.34 and it should be 1.35

  • Salk (unregistered) in reply to Salk

    Function RoundIt(f As Double, DecPlaces As Integer) As String
    If DecPlaces <= 0 Then
    f = Int(f * (10 ^ DecPlaces) + 0.5)
    f = f / (10 ^ DecPlaces)
    RoundIt = f
    Else
    RoundIt = Format(f, "#." & Left("00000000000", DecPlaces))
    End If
    End Function



    Anonymous:
    This code doesnt work good.. for example if u round 1.3445  with 2 decimals u would get 1.34 and it should be 1.35




  • (cs) in reply to Salk
    Anonymous:
    Function RoundIt(f As Double, DecPlaces As Integer) As String
    If DecPlaces <= 0 Then
    f = Int(f * (10 ^ DecPlaces) + 0.5)
    f = f / (10 ^ DecPlaces)
    RoundIt = f
    Else
    RoundIt = Format(f, "#." & Left("00000000000", DecPlaces))
    End If
    End Function



    Anonymous:
    This code doesnt work good.. for example if u round 1.3445  with 2 decimals u would get 1.34 and it should be 1.35






    WTF??? 1.3445 should round to 1.35??? Under what circumstances?
  • (cs) in reply to G Dawg

    The biggest WTFs are "this code works" comments ;)

  • 3j (unregistered) in reply to Anon
    Anonymous:
    Robert,

    Also, VB has the advantage of being 'human readable' - so people who know other languages can still usually read a VB WTF and understand it (and laugh!).


    mmm I have to disagree on that one. To me VB looks like it was written by Literature majors. It's much easier to recognize symbols that are consistant throughout most popular languages rather than sifting through unecessary human nouns and verbs. If anything VB may be more readable by people that aren't familiar with other languages.

Leave a comment on “Round() we go again”

Log In or post as a guest

Replying to comment #:

« Return to Article