• (unregistered)

    And once again, a Java programmer thakes the prize for Most Incredible Dumbass award. Congrats, you are quickly surpassing VB as the language of choice for the incompetent and the clueless.

  • (cs)
    Alex Papadimoulis:

    Brad has actually discovered what appears to be the second most useless Java class written [...]

    I can't say this class is useless, the rounding method is just too poorly done but sometimes you need to round a double by a given number of decimal places. The string plumbing to get the rounded number is a WTF but I don't agree about the purpose of this class begin useless. A better way to round a double would be here:

    http://www.rgagnon.com/javadetails/java-0016.html

  • (unregistered)

    Wow... Just WOW.  <font size="3">WTF  [:@]


    ------
    "The Brain Fart is no excuse for poor programming"
    </font>

  • (cs)

    Alex Papadimoulis:

    <FONT color=red>public</FONT> <FONT color=red>static</FONT> String toString<FONT color=blue size=+1>(</FONT> <FONT color=red>double</FONT> d<FONT color=blue size=+1>,</FONT> <FONT color=red>int</FONT> place <FONT color=blue size=+1>)</FONT> <FONT color=blue size=+1>{</FONT>

     

    "Heh, heh, he said double D" - Butthead

    </childish humor>

  • (unregistered) in reply to

    Maybe there are more good Java programmers out there finding and reporting the bad Java code.  If one application has a higher bug count than another, is it more buggy or is it being tested better?

  • (cs) in reply to

    This class is a common workaround for the failure of most programming languages to round the way people think they ought to. Note that this code will ALWAYS round .5 up rather than to the nearest even number (which is standard and statistically sound rounding behaviour). Why a string? Well, that's to avoid any possible decimal-to-binary-to-decimal conversion errors that would have occurred if you try to do it all with actual numbers. You know -- wa-a-ay down in the depths of the decimal places.

    It all makes sense if you approach math with some bad assumptions.

  • (cs)

    Guayo, I have to say your comments annoy me on a daily basis.

    "the rounding method is just too poorly done"

    "A better way to round a double would be here:"

    You do realize that you're basically saying that the person took a simple idea (for which a solution already exists) and made it overcomplicated and obtrusive. Tell me then, what exactly is your definition of a "WTF"? For me, it's horribly ugly code that actually made its way into a system that is actually used. I don't care if there is a valid use for this in some bizarre situation that you can potentially imagine, the fact is that it is way overblown and the coder barely had a clue as to what he was doing.

    If you only want screwed-up code examples that are broken, any of us could volunteer our time at a beginning-level C++ class and start pulling homework that students turn in. The point is to find code that somehow actually works and has been in use for some time.

    WTF 100%.

  • (cs)

    Granted this is a WTF, but Java's NumberFormat 'rounding' facility is a bigger WTF.

    For example:

            double a = 123.650;
            NumberFormat nf = NumberFormat.getNumberInstance();
            nf.setMaximumFractionDigits(1);
            System.out.println( nf.format(a) );

    Alex's reference, complicated and cpu intensive as it is may be the best way to round numbers in Java.

    For those who are lazy, Java prints 123.6 where Excel prints 123.7 for =round(123.650,1). Java will return 123.7 for 123.651, however.

    Oh, and the Java docs don't even mention rounding in reference to setMaximumFractionDigits.

  • (unregistered) in reply to Manni
    
    
    Guayo, I have to say your comments annoy me on a daily basis.


    Couldn't agree more, Manni. Guayo's purpose on this board seems to be to annoy people by imposing his definition of a WTF on everyone, and trying to explain away obviously incredibly dumb coding by reference to some obscure hypothetical situation that the programmer could never have thought of themselves in a million years.

    Perhaps we should just enjoy his comments as a different sort of WTF?

    By the way - I've come across people who put loads of spaces in everywhere like this example. Makes things a nightmare to follow!



  • (cs) in reply to Rick

    I meant Guayo's reference, not Alex.

    Regardless of Guayo's attitude ( which I don't mind ) he did give good programming advice.

  • (cs) in reply to Manni

    >Guayo, I have to say your comments annoy me on a daily basis.

    Wow!... I feel overwhelmed for the effect of my comments on you. [;)]

    >You do realize that you're basically saying that the person took a simple idea
    >(for which a solution already exists) and made it overcomplicated and obtrusive.

    That's not true, in the standard Java API there isn't a function to round a double by a given number of decimal places.
    Yes you can do that easily but the key is that you have to do something to get that kind of rounding...

    >Tell me then, what exactly is your definition of a "WTF"? For me, it's horribly
    >ugly code that actually made its way into a system that is actually used. I don't
    >care if there is a valid use for this in some bizarre situation that you can
    >potentially imagine, the fact is that it is way overblown and the coder barely
    >had a clue as to what he was doing.

    In my post I did said this was a WTF, I just wasn't agreeing with this class being useless, this class is very useful and I'm not talking of bizarre situations.
    This WTF code is very useful, it's just that is poorly implemented. Hell, the ObjectWrapper class could be useful (that was addressed in that post).

    >If you only want screwed-up code examples that are broken, any of us could volunteer
    >our time at a beginning-level C++ class and start pulling homework that students turn in.
    >The point is to find code that somehow actually works and has been in use for some time.
    >WTF 100%.

    Again, re read my post and tell me where did I suggest this wasn't a WTF.

  • (cs) in reply to Rick
    Rick:

    Granted this is a WTF, but Java's NumberFormat 'rounding' facility is a bigger WTF.

    For example:

            double a = 123.650;
            NumberFormat nf = NumberFormat.getNumberInstance();
            nf.setMaximumFractionDigits(1);
            System.out.println( nf.format(a) );

    Alex's reference, complicated and cpu intensive as it is may be the best way to round numbers in Java.

    For those who are lazy, Java prints 123.6 where Excel prints 123.7 for =round(123.650,1). Java will return 123.7 for 123.651, however.

    Oh, and the Java docs don't even mention rounding in reference to setMaximumFractionDigits.

    There is a difference between Java and Excel's behaviour all right. Java's rounding is canonical; Excel's is wrong. There are nine values that are rounded (1 through 9, zero is not rounded by definition). In any statistically-sound rounding scheme, four numbers (1 to 4) are always rounded down, four (6 to 9) are always rounded up, and 5 is rounded to the nearest even number to minimize accumulated errors.

  • (cs) in reply to Guayo

    When I said that a solution already existed, I was referencing the link you provided. If I find that the language I'm using has a limitation or a quirkiness about it, the first thing I do is hit Google and find out if other people have run into the same thing. There are much better programmers out there than me, I'd rather use well-written, existing code for situations like this and let me spend my time worrying about the rest of my task.

    And you're saying the code was poorly implemented but very useful. I wouldn't call WTF code "useful". I suppose that's the basis of my confusion. Exactly what is your opinion? It sounds to me like you want very much to agree that this is retarded code, but you can't help but disagree with us like you do on 99% of the WTFs posted.

  • (cs) in reply to Manni

    <FONT size=3><FONT face="Times New Roman">Ok... to clarify my position…<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p></FONT></FONT>

    <FONT size=3><FONT face="Times New Roman">The implementation sucks… it's a WTF no less.<o:p></o:p></FONT></FONT>

    Rounding to a given a number of decimal places it's a very common need so the class purpose it's very useful.

    For me: usefulness != correctness...

  • (cs) in reply to Stan Rogers

    int
    main( int argc, char **argv ) {
        printf( "%10.1f\n", 123.65 );
    }

    <FONT style="BACKGROUND-COLOR: #efefef">The C language is also wrong, like Excel ????</FONT>

  • (cs) in reply to Rick

    Yep, if it's rounding doesn't follow statistically-sound practices. Just because it's old and venerated doesn't make it right -- feel free to consult anyone who deals in physical sciences, engineering or finance if you don't believe me.

  • (cs) in reply to Stan Rogers

    Stan Rogers:
    Yep, if it's rounding doesn't follow statistically-sound practices. Just because it's old and venerated doesn't make it right -- feel free to consult anyone who deals in physical sciences, engineering or finance if you don't believe me.

    The term canonical is defined thusly, 'Conforming to orthodox or well-established rules or patterns, as of procedure'

    Are you saying that the standard set by, 'C', 'C++', perl, awk, Excel is NOT a well-established pattern????

    Does anyone know of another language that rounds like Java?? Its been a while since I have done Fortran or COBOL or Forth, but I think I would remember it the worked differently.

     

  • (cs) in reply to Rick
    Rick:

    [image] Stan Rogers wrote:
    Yep, if it's rounding doesn't follow statistically-sound practices. Just because it's old and venerated doesn't make it right -- feel free to consult anyone who deals in physical sciences, engineering or finance if you don't believe me.

    The term canonical is defined thusly, 'Conforming to orthodox or well-established rules or patterns, as of procedure'

    Are you saying that the standard set by, 'C', 'C++', perl, awk, Excel is NOT a well-established pattern????

    Does anyone know of another language that rounds like Java?? Its been a while since I have done Fortran or COBOL or Forth, but I think I would remember it the worked differently.

     

     

    And "canonical" should refer to the disciplines in which the ritual is practiced. Computing doesn't "use" rounding, it performs calculations which may (or may not) provide correct results to people who actually practice disciplines that use rounding. If what C (or C++) provides natively would be considered incorrect by every physical scientist, engineer, statistician and actuary on the planet for well-explored and sound mathematical reasons, then it is wrong. Being old makes it traditional, not canonical.

  • (unregistered)

    C'mon people. Let's not get caught up on semantics. Canonical or traditional...who cares. Duh. It's all so pointless. Let's get back to evaluating the code.

  • (unregistered)

    Hey everybody...the true WTF is that all of these programs/programming languages can't agree on one single way to round a number. How stupid is that.

  • (cs) in reply to

    As far as I know, all computer languages agree except for Java. Somebody prove me wrong.

  • (unregistered)

    Guayo: regarding your usefulness != correctness comment...I don't agree with that logic. If something is useful, being used, and profitable for a production environment, then it's really a moot point if it is "correct" or not. The fact that it is useful makes it correct. Does this make sense?

  • (unregistered) in reply to Stan Rogers

    VB 6, and all .Net lanugages including C# do the same rounding as Java. Microsoft calls it Bankers Rounding.
    The behavior of this method follows IEEE Standard 754, section 4. This kind of rounding is sometimes called rounding to nearest, or banker's rounding.
    And YES its a pain in the A$$ because SQL Server and Crystal do .5 up rounding

  • (unregistered) in reply to Rick

    Rick> Granted this is a WTF, but Java's NumberFormat 'rounding' facility is a bigger WTF.

    NumberFormat is used for formatting a number as a string.  Not for rounding.  There are other more flexible API's for that.

  • (cs)

    Rick, there's only one reason why you believe that all .5 rounding should go up. All of the languages listed are just looking for support of your intuition. Why? Because your elementary school teacher told you that's the way things rounded in 5th grade. And maybe a parent. That's it. And we all know that everything we learn as kids holds true in the Real World, don't we? Just because that 'intuition' was translated into behavior in several computer languages doesn't make it any more correct than IE's/NS4's treatment of modern web standards.

    Sometimes we have to adjust preconceptions as we grow up, in order to obtain more rational, correct results. This is one of those times.

  • (cs) in reply to
    :

    C'mon people. Let's not get caught up on semantics. Canonical or traditional...who cares. Duh. It's all so pointless. Let's get back to evaluating the code.

     

    While the code is, um, less-than-optimal, the underlying "problem" it appears to be trying to solve is the .5-case of rounding. So a discussion of rounding (or, rather, the expectations thereof) is germaine to an assessment of the relative goodness/badness of the code.

    Disclaimer: I do not support the use of strings to manipulate numbers or date-time values; if the string is ever used for anything other than UI display, the developer responsible should be hung, falyed, drawn and quartered (in that order).

  • (cs) in reply to foxyshadis

    That said, of course there are time you would need rounding one way or the other, as a requirement in a particular existing system. In that case, a much simpler method such as Guayo posted is preferred over a typical make-your-own-buggy-rounding exercise.

        double r = 3.1537;
    int decimalPlace = 2;
    BigDecimal bd = new BigDecimal(r);
    bd = bd.setScale(decimalPlace,BigDecimal.ROUND_HALF_UP);
    r = bd.doubleValue();

  • (cs) in reply to foxyshadis

    That pretty much sims up the average OO WTF -- people are not aware of or do not understand classes that implement the functionality required. That's why I can (not so proudly) label my own Java as "sparse" -- merely knowing the syntax and root types of a language is not nearly enough. Throwing a value to a BigInteger or BigDecimal gains you a lot of tools you wouldn't have with int or float with or without invoking Math. I'm sure I've rewritten a lot of "core" functionality along the way (although, to be fair, most of what I write is done in JDK 1.1.8, so there's not a lot of "there" there anyway). I just hope I haven't done anything that would wind up here lately.

  • (cs) in reply to Stan Rogers

    Even a superficial reading of my posts today will tell one and all that I'm having a little bit of trouble at the keyboard. Sorry -- I'm having an arthritic episode.

  • (cs) in reply to

    :
    Guayo: regarding your usefulness != correctness comment...I don't agree with that logic. If something is useful, being used, and profitable for a production environment, then it's really a moot point if it is "correct" or not. The fact that it is useful makes it correct. Does this make sense?

    That makes sense... when I said correctness I was thinking more about the underlying implementation and less about the overall result.
    However you could say something is correct if it gives a right solution for a given problem... that code (awkward, clumsy, and maybe stupid) gives a right solution for what it seems to be the problem... so you could say it's correct... I'll re arrange my formula to:

    stupid != useless

  • (cs) in reply to Rick

    Rick:
    As far as I know, all computer languages agree except for Java. Somebody prove me wrong.

    As someone said: .Net does this type of rounding...
    Supposedly in Whidbey we’ll have more options...

  • (unregistered) in reply to
    By the way - I've come across people who put loads of spaces in everywhere like this example. Makes things a nightmare to follow!

    A damning admission - your brain can't ignore empty space. You deserve a big WTF of your very own.

    Different people have different coding styles - grow up and get used to it.

  • (unregistered)

    hey alex, whatever code highlighting tool you're using... adjust it er something, cause the listing looks crazy in Firefox -- the braces and parens are way huge compared to the actual code.

  • (unregistered)

    <FONT style="BACKGROUND-COLOR: #efefef">This is one of those things that the Bignumber class in Java gets right (it's got more rounding options than I've heard of) It's also something that is now fixed in C# V2 as I moaned about it (yea, for me I got MS to add a function). You can't use bankers rounding for rounding in VAT calculations in Europe so it means you end having to write your own routines. More irritatingly still for C# programmers if you use the currency formatter it does the .5 or higher style rounding.</FONT>

    <FONT style="BACKGROUND-COLOR: #efefef">Bankers rounding is only useful for stats, for accounting purposes it's a pain.</FONT>

  • (unregistered) in reply to Rick
    Rick:

    Does anyone know of another language that rounds like Java?? Its been a while since I have done Fortran or COBOL or Forth, but I think I would remember it the worked differently.

    Rounding in COBOL? (it's been 8 years for me too)

    You can do redefines, essentially 'hack-it-off rounding'

  • (unregistered)

    I think that I've already seen this link on this site :

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

    This should help everyone to understand that there is no wrong rounding (like Excel one) or good one. There is only rounding that suits particular needs.

  • (unregistered)

    If it makes you feel any better, rounding procedures should only ever be used for displaying the values to humans for readability, they should not be used in programming

    Rounding up a large list of values and then adding them is in  itself a WTF, if you need to round, do it at the end of the operation, not at the beginning.

    In that case, it doesn't actually make any significant difference which rule is used for rounding.

  • (unregistered) in reply to Rick
    Rick:
    As far as I know, all computer languages agree except for Java. Somebody prove me wrong.

    dotNET also rounds the same way as Java.  I know 'cos I had to write my own little routine to round .5 up (which is the expected behaviour for that particular app).
  • (unregistered)

    After all of this discussion about rounding and how difficult it really is, I decided to create my own programming language with a rounding function. (BTW, I don't remember rounding being that difficult in grade school...but things change I guess.)  My programming language's round function will go against all conventional thinking about how to actually round a number. I was thinking and thinking about how to do this, when it hit me. We already have a language like that...it's called Java.

  • (unregistered)

    I think that SQL and C# round the same way...  It makes no sense to me...

    If you want to make sure that .5 rounds up you need to write your own method.

    If I'm wrong please let me know... because I waisted 5 minutes of company time writing my own rounding function (better than the one above).

  • (unregistered)

    I know why one language rounds differently than another. It's because the language was written by someone who wanted to be "creative" and have a bunch of techno-nerds follow him and his outlandish theories. That was back in 1995 when programming was more or less still considered a "nerdy" profession. Nowadays, programming is not "nerdy", it is simply a job that puts food on the table. Today's programmers want functions that work. Reliable, intuitive stuff. We don't want theoretical, think-outside-the-box garbage (i.e. the whole be-different-just-to-be-different thing). I find it really pathetic that we have become such a subjective society that we can't agree on how to round a number. That's really sad man.

  • (unregistered)

    Why are people still posting about how this rounding is so illogical? It was explained very early on. I guess some people still have trouble letting go of the things they learnt when they were 5.

    Here's an example for those of you who think Mrs Smith back in kindergarten is smarter than most financial developers: I have two numbers, 3.25 and 3.35. I want to add them, but they are rounded to 1dp first. Note that the correct sum is 6.6.

    With primary school rounding, I add 3.3 and 3.4 and get 6.7. With banker's rounding, I add 3.2 and 3.4 and get.... 6.6.

    For the sake of adding two numbers, this is hardly a compelling argument, but please use your brains and extend this to millions of transactions per day. An error like that for every two numbers rounded adds up to significant amounts on that scale. The idea is that statistically, half of those numbers will have even digits before the rounded digit, and thus will be rounded down, so the whole thing evens out to be much more accurate.

  • (unregistered) in reply to

    (Oh, and for those of you who say the numbers shouldn't be rounded until they've been added - absolutely impractical, especially if you're dealing with, for example, 1/3 of a dollar). Obviously the numbers the bank works with are much more accurate than 1 or 2dp, but computers are finite machines, and can't work with inifinitely long decimals, and on the scale of a large bank, even a rounding error of 0.000000001 per transaction is going to be fairly significant at the end of the month.

  • (cs)

    World peace could be achieved if only people would use my round around function.

    round = Math.round(number + (Math.random * Math.PI /3) - (System.Timer.microseconds()/50000) * (int)System.Web.HttpWebServicesGet('www.foxyshadis.com/number.html'))

    Or something like that, written in my bizzare Java/C#/VB.Net/PHP conglomeration.

  • (unregistered) in reply to
    :
    hey alex, whatever code highlighting tool you're using... adjust it er something, cause the listing looks crazy in Firefox -- the braces and parens are way huge compared to the actual code.

    Take a look at this:
    <BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"><PRE><FONT color=red><B>class</B></FONT> Rounding <FONT color=blue size=+1><B>{</B></FONT>
    <FONT color=red><B>public</B></FONT> <FONT color=red><B>static</B></FONT> String toString<FONT color=blue size=+1><B>(</B></FONT>

    IMHO, the code on this site is a WTF in itself. [:)]
  • (unregistered) in reply to

    Funny thing is, that the wost Java code I ever see is code witten by a Perl/C/VB Programmer. Not by a tried and tested Java Programmer (though there are enough WTF's around for them as well however). But ingeneral a perl programmer writing Java tries to make the Java look like Perl... now there is a WTF waiting to happen (umm... already did happen).

  • Andreasbmg (unregistered)
    Comment held for moderation.
  • Zelenakbs (unregistered)
    Comment held for moderation.
  • Zelenazyc (unregistered)
    Comment held for moderation.
  • Zelenahkz (unregistered)
    Comment held for moderation.

Leave a comment on “My Very Own math.round()”

Log In or post as a guest

Replying to comment #26989:

« Return to Article