Clever, but still not useful



  • function floor(number)
    {
      return Math.floor(number*Math.pow(10,2))/Math.pow(10,2);
    }

        +1 for creating a method that returns a number with 2 decimal places, as long as number has at least 2 decimal places

        -1 for naming that method after a built in function

        -1 for using a name that incorrectly describes what the method does

        -1 for unnecessary use of pow on constanst numbers

        -1 for unnecessary use of pow on constanst numbers a second time

        -1 for not making sure sure that it always returns a number with 2 decimal places 



  • Wow... I'd like to know the thought process that goes with writing something like this.



  • @dphunct said:

    -1 for not making sure sure that it always returns a number with 2 decimal places 
    I think this doesn't necessarily apply. It could be a method to round to two decimal places, and not necessarily to format the value. It may just not be named correctly, as you already pointed out above.



  • It looks like it is used to round off internal calculations to two decimal places.  Maybe they are calculations involving money?  It certainly doesn't format to 2 decimal places, since most numeric variables do not have different representations of 100.00 and 100.  The extra two zeros only appear when formatted as a string for output.

    It still looks like it was copied from a first-year textbook using Math.pow(), which obviously should be replaced with a literal or a constant.  So long as this is the only function in the system used to round off the cents, then it's not too bad but I bet there'll be some other confusingly-named function elsewhere that rounds to 3 places "for extra precision."

    Please tell me this isn't used in a real application handling real money.



  • @Qwerty said:

    It looks like it is used to round off internal calculations to two decimal places.  Maybe they are calculations involving money?  It certainly doesn't format to 2 decimal places, since most numeric variables do not have different representations of 100.00 and 100.  The extra two zeros only appear when formatted as a string for output.

    It still looks like it was copied from a first-year textbook using Math.pow(), which obviously should be replaced with a literal or a constant.  So long as this is the only function in the system used to round off the cents, then it's not too bad but I bet there'll be some other confusingly-named function elsewhere that rounds to 3 places "for extra precision."

    Please tell me this isn't used in a real application handling real money.

    Money should ALWAYS be done in integer-based formats. An integer representing pennies instead of dollars, or an integer pair with overflow check... The closest to "binary floating point" currency should ever get is Binary Coded Decimal. If he's actually using floats for money, that's the real WTF. (And grounds for legal penalties in more than a few districts should rounding error ever reach the real world or a real account balance...)



  • @DaEagle said:

    Wow... I'd like to know the thought process that goes with writing something like this.
     

    I think with appropriate intake of LSD and booze, you can experience it yourself. 



  • @Ilya Ehrenburg said:

    @DaEagle said:

    Wow... I'd like to know the thought process that goes with writing something like this.
     

    I think with appropriate intake of LSD and booze, you can experience it yourself. 

     

    public bool IsAppropriateAmount(double ouncesOfBooze, int hitsOfAcid)

    {

    double total = ouncesOfBooze * hitsOfAcid;

    return (total > 0.0 && total < enoughToKillYou);

    }



  • @Ilya Ehrenburg said:

    I think with appropriate intake of LSD and booze
    Or by smoking beard-of-the-Swampling



  • Also -1 for not even getting it right. If the arithmetic is in doubles then floor(0.29) = 0.28

    Also -1 for not even getting it idempotent: floor(floor(0.58)) = floor(0.57) = 0.56.

    I hate floating point.



  •  @Wolftaur said:

    Money should ALWAYS be done in integer-based formats. An integer representing pennies instead of dollars, or an integer pair with overflow check... The closest to "binary floating point" currency should ever get is Binary Coded Decimal. If he's actually using floats for money, that's the real WTF. (And grounds for legal penalties in more than a few districts should rounding error ever reach the real world or a real account balance...)

    BigDecimal ftw


Log in to reply