Multiplying Strings

« Return to Article
  • alo 2012-12-06 08:04
    ot*"0.Frist"

    Good thing is that even discount percentages like 1000% wouldn't make the final price negative...
  • Nobody 2012-12-06 08:06
    Lame


    Additional text to avoid spam filter
  • ochrist 2012-12-06 08:07
    alo:
    Am I frist this time?

    Yeah, but you didn't get the joke. You should have made a long comment....
  • alo 2012-12-06 08:10
    The "frist" strategy is to be logged in, writing a comment as fast as you can and the edit it later to something less embarrasing.
  • ¯\(°_o)/¯ I DUNNO LOL 2012-12-06 08:13
    TRWTF is that some people shouldn't be allowed near anything that requires 9th grade mathematics skills. This isn't even algebra.

    VB? How am I not surprised? (It was either that or PHP.)
  • alvatrus 2012-12-06 08:16
    Which makes *every* poster of *any* top comment a loser by default.
  • Cbuttius 2012-12-06 08:17
    The person who wrote that had the right buzzwords on their CV - they'd played with the right toys.

    And they probably had nice personalities as well and came across well at their interviews.

    Of course using string conversion is a total WTF. That the code also doesn't work actually gives him grounds to change it, so perhaps better than if it were written correctly.

    The code appears to not work because they forget that you need 2 digits to represent the percent so it is giving a discount of 0.5 when it should be 0.05?

  • AGray 2012-12-06 09:07
    ¯\(°_o)/¯ I DUNNO LOL:
    TRWTF is that some people shouldn't be allowed near anything that requires 9th grade mathematics skills. This isn't even algebra.

    VB? How am I not surprised? (It was either that or PHP.)


    I digress...but, why is it most of the more horrendously stupid WTFs are written in VB? I have never really understood it.
  • b_russel 2012-12-06 09:10
    I wonder what would have happened if the original programmer would have fixed this bug: Would he have added a few PadLeft and IndexOf calls, and a regular expression or two for good measure? ;-)
  • the beholder 2012-12-06 09:15
    Cbuttius:
    The person who wrote that had the right buzzwords on their CV - they'd played with the right toys.

    And they probably had nice personalities as well and came across well at their interviews.

    Of course using string conversion is a total WTF. That the code also doesn't work actually gives him grounds to change it, so perhaps better than if it were written correctly.

    The code appears to not work because they forget that you need 2 digits to represent the percent so it is giving a discount of 0.5 when it should be 0.05?

    Ooookay... that is what is missing. I wasn't sure what was the bug until I read your post.

    Yes people, the code is awful, but it at least is written in a way that's supposed to calculate the right amount (except when it fits Cbuttius' example). If you feed it a 10% discount or higher it will perform correctly.

    It's the stupider, bug-prone route to the right place.
  • Foo Bar 2012-12-06 09:35
    AGray:
    I digress...but, why is it most of the more horrendously stupid WTFs are written in VB? I have never really understood it.

    Possibly because some Peters rise up to their level of incompetence by way of:
    use MS Office products -> write a macro or two -> hey, that's Visual Basic -> VB programmer.
  • Peter Bouillon 2012-12-06 09:41
    Yes, of course this code is to blame.

    * If O.DiscountPercentage is lower than 10, then the calculated discount will off by a factor of 10.

    * If O.DiscountPercentage contains a decimal point (e.g. "10.5"), the digits behind it will be lost.

    * If O.DiscountPercentage is negative, then "0." & Convert.ToInt32(O.DiscountPercentage) will result in nonsense.
  • Peter Bouillon 2012-12-06 09:45
    the beholder:

    If you feed it a 10% discount or higher it will perform correctly.


    It won't work with a 100% discount.
  • very punny 2012-12-06 09:48
    [quote] but one thing was for certain - this was going to be a long<!--Get it? Long...Decimal... When was the last time you saw a data type pun?--> week. [\quote]

    He was going to need to use a float-ing holiday after this.
  • TheSHEEEP 2012-12-06 09:50
    Foo Bar:
    AGray:
    I digress...but, why is it most of the more horrendously stupid WTFs are written in VB? I have never really understood it.

    Possibly because some Peters rise up to their level of incompetence by way of:
    use MS Office products -> write a macro or two -> hey, that's Visual Basic -> VB programmer.


    Also, it is (or was, rather, guess today it is JavaScript) used in many schools for computer science lessons.
  • Pokepoke 2012-12-06 10:20
    This sounds like a beginning of a journey... Cant wait for chapter 2 to come!
  • RRDY 2012-12-06 10:35
    But dividing by 100 is HARD!

    captcha: nobis, as in this person had nobis getting anywhere near a computer.
  • Herwig 2012-12-06 10:43
    Foo Bar:
    AGray:
    I digress...but, why is it most of the more horrendously stupid WTFs are written in VB? I have never really understood it.

    Possibly because some Peters rise up to their level of incompetence by way of:
    use MS Office products -> write a macro or two -> hey, that's Visual Basic -> VB programmer.

    ...and then they are talking about the "Storage Procedures" they've written in "Virtual Basic" and their other programming skills in in HTML or FTP...
  • Valetudo 2012-12-06 10:47
    Herwig:
    Foo Bar:
    AGray:
    I digress...but, why is it most of the more horrendously stupid WTFs are written in VB? I have never really understood it.

    Possibly because some Peters rise up to their level of incompetence by way of:
    use MS Office products -> write a macro or two -> hey, that's Visual Basic -> VB programmer.

    ...and then they are talking about the "Storage Procedures" they've written in "Virtual Basic" and their other programming skills in in HTML or FTP...


    In the final stages, they talk about writing a gui in VB to trace an IP.
  • Someone 2012-12-06 11:00
    Just back from the trauma center, no further comments.
  • lethalronin27 2012-12-06 11:06
    Peter Bouillon:
    the beholder:

    If you feed it a 10% discount or higher it will perform correctly.


    It won't work with a 100% discount.


    Or any percentage with a decimal, like 33.33%
  • Coyne 2012-12-06 11:39
    RRDY:
    But dividing by 100 is HARD!

    captcha: nobis, as in this person had nobis getting anywhere near a computer.


    Inefficient, too.

    Had a teacher one time that assigned us to minimize parenthesis in something like this:

     A + ((B * C) * (D * E)) / ((F * G) * H)


    My response:

     A + B * C * D * E / F / G / H


    Whereupon he objected lamely that division was inefficient...but he gave in when I pointed out that the whole objective of the problem was to get rid of parenthesis.
  • Spudley 2012-12-06 11:55
    [quote user="very punny"][quote] but one thing was for certain - this was going to be a long<!--Get it? Long...Decimal... When was the last time you saw a data type pun?--> week. [\quote]

    He was going to need to use a float-ing holiday after this.
    [/quote]

    This int what I was expecting. I object that they string me along just for a punchline like that! I'll have to double my efforts to come up with a suitable array of ripostes.
  • chubertdev 2012-12-06 11:56
    One thing was for certain - this was going to be a long/*Get it? Long...Decimal... When was the last time you saw a data type pun?*/ week.


    It's been a Short while.
  • Tankster 2012-12-06 12:03
    [quote user="very punny"][quote] but one thing was for certain - this was going to be a long<!--Get it? Long...Decimal... When was the last time you saw a data type pun?--> week. [\quote]

    He was going to need to use a float-ing holiday after this.
    [/quote]

    I would string up the developer who did this
  • D-Coder 2012-12-06 12:04
    How is it that no one had noticed a problem with discounts less than 10% for so long?
  • lunaryorn 2012-12-06 12:25
    D-Coder:
    How is it that no one had noticed a problem with discounts less than 10% for so long?

    It seems to be a very generous company... which might explain the quality of the code: There seems to be not enough money left to replace the Office expert dabbling on programming with a real, appropriately paid developer.
  • Oscar 2012-12-06 12:26
    D-Coder:
    How is it that no one had noticed a problem with discounts less than 10% for so long?
    Maybe it is one of those shady businesses that marks everything down 40% or more routinely.

    Scam alert #1, for you naive youngsters, just in time for the holidays: jewelry stores price their $1000 rings at $2000 then give you "50% off", so you can impress your girlfriend how much you're (not really) spending on her. The problem is, she's much more tuned in to jewelry prices than you are, having spent 20 years now obsessing on the topic. So you're really not fooling her.

    Scam alert #2: once you do buy that ring, and complete the related ceremonies, you can start counting down on one hand how many more times in your life you're going to get laid by a hot chick instead of a grudging grump.
  • da Doctah 2012-12-06 12:37
    Peter Bouillon:
    Yes, of course this code is to blame.

    * If O.DiscountPercentage is lower than 10, then the calculated discount will off by a factor of 10.

    * If O.DiscountPercentage contains a decimal point (e.g. "10.5"), the digits behind it will be lost.

    * If O.DiscountPercentage is negative, then "0." & Convert.ToInt32(O.DiscountPercentage) will result in nonsense.


    And if O.DiscountPercentage is 100 or greater, the calculated discount will be too small.
  • Kae 2012-12-06 12:39
    Someone's going to pull some double shifts. It's going to get a bit hectic. But stuff like that builds character. It shows class. Nothing to object to.
  • The Big Picture Thinker 2012-12-06 12:44
    TheSHEEEP:
    Foo Bar:
    AGray:
    I digress...but, why is it most of the more horrendously stupid WTFs are written in VB? I have never really understood it.

    Possibly because some Peters rise up to their level of incompetence by way of:
    use MS Office products -> write a macro or two -> hey, that's Visual Basic -> VB programmer.


    Also, it is (or was, rather, guess today it is JavaScript) used in many schools for computer science lessons.

    Or Java or C#, which is essentially VB with curly brackets
  • chubertdev 2012-12-06 13:08
    The Big Picture Thinker:
    Or Java or C#, which is essentially VB with curly brackets


    And case sensitivity. Which is the biggest WTF in any programming language.
  • C-Derb 2012-12-06 13:56
    da Doctah:
    Peter Bouillon:
    Yes, of course this code is to blame.

    * If O.DiscountPercentage is lower than 10, then the calculated discount will off by a factor of 10.

    * If O.DiscountPercentage contains a decimal point (e.g. "10.5"), the digits behind it will be lost.

    * If O.DiscountPercentage is negative, then "0." & Convert.ToInt32(O.DiscountPercentage) will result in nonsense.


    And if O.DiscountPercentage is 100 or greater, the calculated discount will be too small.
    A discount > 100% would be paying someone to take your products off your hands. At that point, you've got bigger problems than string arithmetic.
  • eek 2012-12-06 13:59
    [quote user="very punny"][quote] but one thing was for certain - this was going to be a long<!--Get it? Long...Decimal... When was the last time you saw a data type pun?--> week. [\quote]

    He was going to need to use a float-ing holiday after this.
    [/quote]

    Your String of data type puns disgusts me.
  • Tasty 2012-12-06 14:28
    Coyne:
    RRDY:
    But dividing by 100 is HARD!

    captcha: nobis, as in this person had nobis getting anywhere near a computer.


    Inefficient, too.

    Had a teacher one time that assigned us to minimize parenthesis in something like this:

     A + ((B * C) * (D * E)) / ((F * G) * H)


    My response:

     A + B * C * D * E / F / G / H


    Whereupon he objected lamely that division was inefficient...but he gave in when I pointed out that the whole objective of the problem was to get rid of parenthesis.


    I imagine a modern compiler's optimizer will reduce your code to something more efficient. It's not your father's Fortran anymore.
  • foo 2012-12-06 14:51
    Coyne:
    RRDY:
    But dividing by 100 is HARD!

    captcha: nobis, as in this person had nobis getting anywhere near a computer.


    Inefficient, too.

    Had a teacher one time that assigned us to minimize parenthesis in something like this:

     A + ((B * C) * (D * E)) / ((F * G) * H)


    My response:

     A + B * C * D * E / F / G / H


    Whereupon he objected lamely that division was inefficient...but he gave in when I pointed out that the whole objective of the problem was to get rid of parenthesis.
    Assuming these were floating point numbers, he should have objected that you may have changed the result as the associate law doesn't hold. What's safe to remove is:
     A + B * C * (D * E) / (F * G * H)

  • A. Nonymous 2012-12-06 14:53
    alvatrus:
    Which makes *every* poster of *any* top comment a loser by default.

    That's why I usually start reading after the frist three or so comments...
  • Zylon 2012-12-06 14:56
    I'm not sure "copypasta" means what Mark thinks it means.
  • foo 2012-12-06 14:56
    chubertdev:
    The Big Picture Thinker:
    Or Java or C#, which is essentially VB with curly brackets


    And case sensitivity. Which is the biggest WTF in any programming language.
    Yeah, it really makes more sense to be able to write a program that compiles differently in a Turkish locale. (I'll spare you the details since it's been discussed to often and you can google.)
  • j.albert 2012-12-06 15:04
    Hopefully he didn't have a short temper...
  • foo 2012-12-06 15:04
    [quote user="eek"][quote user="very punny"][quote] but one thing was for certain - this was going to be a long<!--Get it? Long...Decimal... When was the last time you saw a data type pun?--> week. [\quote]

    He was going to need to use a float-ing holiday after this.
    [/quote]

    Your String of data type puns disgusts me.[/quote]Sadly, these unsigned comments always follow the same template. Let's go booling.
  • chubertdev 2012-12-06 15:07
    foo:
    chubertdev:
    The Big Picture Thinker:
    Or Java or C#, which is essentially VB with curly brackets


    And case sensitivity. Which is the biggest WTF in any programming language.
    Yeah, it really makes more sense to be able to write a program that compiles differently in a Turkish locale. (I'll spare you the details since it's been discussed to often and you can google.)


    Did you read "case" as "culture"?
  • foxyshadis 2012-12-06 15:08
    Well, this guy obviously remember the dictum to never use floats for currency... and picked precisely the wrong solution.

    ¯\(°_o)/¯ I DUNNO LOL:
    TRWTF is that some people shouldn't be allowed near anything that requires 9th grade mathematics skills. This isn't even algebra.

    VB? How am I not surprised? (It was either that or PHP.)

    Remember when people who should have known better would earnestly claim that javascript had no addition, and that eval(a+"+"+b) was the only way to do it? Because they screwed up and didn't convert strings from the webform into ints, of course.
  • foo 2012-12-06 15:24
    chubertdev:
    foo:
    chubertdev:
    The Big Picture Thinker:
    Or Java or C#, which is essentially VB with curly brackets


    And case sensitivity. Which is the biggest WTF in any programming language.
    Yeah, it really makes more sense to be able to write a program that compiles differently in a Turkish locale. (I'll spare you the details since it's been discussed to often and you can google.)


    Did you read "case" as "culture"?
    HTFM.

    Case-folding depends on the locale.
  • Richard 2012-12-06 15:26
    [quote user="very punny"][quote] but one thing was for certain - this was going to be a long<!--Get it? Long...Decimal... When was the last time you saw a data type pun?--> week. [\quote]

    He was going to need to use a float-ing holiday after this.
    [/quote]

    Ha, ha. Very guid.
  • Matthew 2012-12-06 15:26
    foxyshadis:
    Well, this guy obviously remember the dictum to never use floats for currency... and picked precisely the wrong solution.

    ¯\(°_o)/¯ I DUNNO LOL:
    TRWTF is that some people shouldn't be allowed near anything that requires 9th grade mathematics skills. This isn't even algebra.

    VB? How am I not surprised? (It was either that or PHP.)

    Remember when people who should have known better would earnestly claim that javascript had no addition, and that eval(a+"+"+b) was the only way to do it? Because they screwed up and didn't convert strings from the webform into ints, of course.


    eval is evil. The right way to add in JavaScript is
    a - -b
  • qbolec 2012-12-06 15:27
    why can't you drop the parnthesis around D*E as well? Is it because of overflows?
  • bjolling 2012-12-06 15:33
    foo:
    eek:
    very punny:
    but one thing was for certain - this was going to be a long<!--Get it? Long...Decimal... When was the last time you saw a data type pun?--> week.
    He was going to need to use a float-ing holiday after this.
    Your String of data type puns disgusts me.
    Sadly, these unsigned comments always follow the same template. Let's go booling.
    Shirley quoting can't be that hard
  • foo 2012-12-06 15:37
    qbolec:
    why can't you drop the parnthesis around D*E as well? Is it because of overflows?
    That, and rounding might be slightly different with a different order of evaluation.
  • uns 2012-12-06 15:48
    [quote user="Spudley"][quote user="very punny"][quote] but one thing was for certain - this was going to be a long<!--Get it? Long...Decimal... When was the last time you saw a data type pun?--> week. [\quote]

    He was going to need to use a float-ing holiday after this.
    [/quote]

    This int what I was expecting. I object that they string me along just for a punchline like that! I'll have to double my efforts to come up with a suitable array of ripostes.
    [/quote]

    for real?
  • PRMan 2012-12-06 17:17
    But a 5% discount becomes a 50% discount! Probably the bug in question.
  • C-Derb 2012-12-06 17:50
    bjolling:
    foo:
    eek:
    very punny:
    but one thing was for certain - this was going to be a long<!--Get it? Long...Decimal... When was the last time you saw a data type pun?--> week.
    He was going to need to use a float-ing holiday after this.
    Your String of data type puns disgusts me.
    Sadly, these unsigned comments always follow the same template. Let's go booling.
    Shirley using the preview button can't be that hard
    FTFY
  • chubertdev 2012-12-06 18:01
    foo:
    HTFM.

    Case-folding depends on the locale.


    Hehe, just messing with you.

    I've read the arguments on both sides, and I'm an ardent supporter of making my own life easier.
  • valdik 2012-12-06 18:02
    [quote user="uns"][quote user="Spudley"][quote user="very punny"][quote] but one thing was for certain - this was going to be a long<!--Get it? Long...Decimal... When was the last time you saw a data type pun?--> week. [\quote]

    He was going to need to use a float-ing holiday after this.
    [/quote]

    This int what I was expecting. I object that they string me along just for a punchline like that! I'll have to double my efforts to come up with a suitable array of ripostes.
    [/quote]

    for real?[/quote]

    Yeah, I would object.
  • chubertdev 2012-12-06 18:30
    Especially since the majority of code, whether you want it like that or not, is English:

    http://msdn.microsoft.com/tr-tr/library/0x9tb07z(v=vs.80).aspx
  • foo 2012-12-06 18:36
    chubertdev:
    foo:
    HTFM.

    Case-folding depends on the locale.


    Hehe, just messing with you.

    I've read the arguments on both sides, and I'm an ardent supporter of making my own life easier.
    Me too, and since using the same identifier with different case is very bad style to me, I'm happy that my compiler (C++) tells me when I get it wrong.
  • foo 2012-12-06 18:38
    chubertdev:
    Especially since the majority of code, whether you want it like that or not, is English:

    http://msdn.microsoft.com/tr-tr/library/0x9tb07z(v=vs.80).aspx
    Thanks for supporting my side (case-sensitivity).

    Think about it: Code written in Turkish works OK in a Turkish locale (obviously). But code written in English will fail there if case-insensitive.
  • chubertdev 2012-12-06 19:02
    foo:
    chubertdev:
    Especially since the majority of code, whether you want it like that or not, is English:

    http://msdn.microsoft.com/tr-tr/library/0x9tb07z(v=vs.80).aspx
    Thanks for supporting my side (case-sensitivity).

    Think about it: Code written in Turkish works OK in a Turkish locale (obviously). But code written in English will fail there if case-insensitive.


    That's just semantics. And it's up to the developer to realize that. The combination of the English programming vernacular and VB both being case-insensitive make it superior. Especially when combined with how well Visual Studio manages it real-time, so you don't have to. I honestly believe that case-sensitive languages, whether it be a programming language like C#, or a language like Turkish, are extremely flawed.
  • Neveralull 2012-12-06 19:02
    alo:
    The "frist" strategy is to be logged in, writing a comment as fast as you can and the edit it later to something less embarrasing.
    You didn't edit this one very well
  • punny too 2012-12-06 19:08
    I'll take your WORD on it.
  • foxyshadis 2012-12-06 19:14
    Coyne:
    RRDY:
    But dividing by 100 is HARD!

    captcha: nobis, as in this person had nobis getting anywhere near a computer.


    Inefficient, too.

    Had a teacher one time that assigned us to minimize parenthesis in something like this:

     A + ((B * C) * (D * E)) / ((F * G) * H)


    My response:

     A + B * C * D * E / F / G / H


    Whereupon he objected lamely that division was inefficient...but he gave in when I pointed out that the whole objective of the problem was to get rid of parenthesis.

    You also could have converted to postfix and really made his day.
  • Silverhill 2012-12-06 19:36
    foo:
    Thanks for supporting my side (case-sensitivity).
    Don't you mean:
    "Please attempt some case-sensitivity: I had a compiler that was non-case-sensitive, and let me assure you it is no laughing matter."

  • gnasher729 2012-12-06 19:41
    Tasty:
    I imagine a modern compiler's optimizer will reduce your code to something more efficient. It's not your father's Fortran anymore.


    Problem is that replacing a / b / c with a / (b * c) for floating-point numbers will most likely change the result due to rounding errors, which makes it illegal to do in many languages (like C, C++, Java, Objective-C).
  • i can has string concat 2012-12-06 20:50
    The problem is that there are a bunch of so-called programmers who learned how to do string concatenation on day 1, and then go on to apply it everywhere for the rest of their career.

    Where I work, we have one guy who does string concatenation in SQL to return a HTML select, and another who does string concatenation in javascript to do date arithmetic.
  • foo 2012-12-06 21:01
    chubertdev:
    foo:
    chubertdev:
    Especially since the majority of code, whether you want it like that or not, is English:

    http://msdn.microsoft.com/tr-tr/library/0x9tb07z(v=vs.80).aspx
    Thanks for supporting my side (case-sensitivity).

    Think about it: Code written in Turkish works OK in a Turkish locale (obviously). But code written in English will fail there if case-insensitive.


    That's just semantics. And it's up to the developer to realize that. The combination of the English programming vernacular and VB both being case-insensitive make it superior. Especially when combined with how well Visual Studio manages it real-time, so you don't have to. I honestly believe that case-sensitive languages, whether it be a programming language like C#, or a language like Turkish, are extremely flawed.
    Sorry, that was a little too much. Until you wrote that I considered you might not be trolling. (Though the use of VB and superior in the same sentence above should have been a give-away.)
  • Darth Paul 2012-12-06 21:05
    chubertdev:
    foo:
    chubertdev:
    Especially since the majority of code, whether you want it like that or not, is English:

    http://msdn.microsoft.com/tr-tr/library/0x9tb07z(v=vs.80).aspx
    Thanks for supporting my side (case-sensitivity).

    Think about it: Code written in Turkish works OK in a Turkish locale (obviously). But code written in English will fail there if case-insensitive.


    That's just semantics. And it's up to the developer to realize that. The combination of the English programming vernacular and VB both being case-insensitive make it superior. Especially when combined with how well Visual Studio manages it real-time, so you don't have to. I honestly believe that case-sensitive languages, whether it be a programming language like C#, or a language like Turkish, are extremely flawed.


    Worse: there are programmers out there who don't realise they are working with case-sensitive language (when it is optional).

    A common error in SQL Server development projects is to install SQL Server as case-insensitive, then write case-insensitive code, thereby risking that said code will not compile on systems where SQL Server is installed case-sensitive.

    A major WTF: case-insensitive is the "default" SQL Server installation state, binary (case-sensitive) is the "recommended" configuration state. Most projects I have seen (including major open source projects) have programmers that have not read that part of the manual.
  • leo c 2012-12-06 22:45
    can anyone explain it, is there something wrong with the code? I dont get it.
  • foo 2012-12-06 23:31
    leo c:
    can anyone explain it, is there something wrong with the code? I dont get it.
    Hint: There's a "page 1" link below. And surprisingly, this link leads to a full page of previous comments, some of which even related to the current WTF.
  • AndyCanfield 2012-12-07 00:54
    Maybe I'm an ignorant savage, but I have a JavaScript program that needs to truncate a (float) number to integer. There is no such JavaScript function. All JavaScript integers are floats. The only way I've figured out how to do it is to convert X to a string and parse the string up to the decimal point.
    var I = parseInt( X.toFixed(3));
    Argh!
  • da Doctah 2012-12-07 01:00
    C-Derb:
    da Doctah:
    And if O.DiscountPercentage is 100 or greater, the calculated discount will be too small.
    A discount > 100% would be paying someone to take your products off your hands. At that point, you've got bigger problems than string arithmetic.
    And with that, I experience yet another flashback to all those times I was told "that'll never happen, so it doesn't matter what you do for that case".

    My usual response to that sort of guidance is "in that case, I'll arrange to code it so that if the thing you say will never happen does happen, it's okay to call you at your personal unlisted number any time of the day or night". They seldom agree to this. They never agree to it a second time.
  • Drak 2012-12-07 01:42
    AndyCanfield:
    Maybe I'm an ignorant savage, but I have a JavaScript program that needs to truncate a (float) number to integer. There is no such JavaScript function. All JavaScript integers are floats. The only way I've figured out how to do it is to convert X to a string and parse the string up to the decimal point.
    var I = parseInt( X.toFixed(3));
    Argh!


    Math.floor(X); // takes off everything beyond the decimal point.
  • foo 2012-12-07 01:46
    Drak:
    AndyCanfield:
    Maybe I'm an ignorant savage, but I have a JavaScript program that needs to truncate a (float) number to integer. There is no such JavaScript function. All JavaScript integers are floats. The only way I've figured out how to do it is to convert X to a string and parse the string up to the decimal point.
    var I = parseInt( X.toFixed(3));
    Argh!


    Math.floor(X); // takes off everything beyond the decimal point.
    Not for negative numbers if it's like floor() in most other languages.
  • Jeff Grigg 2012-12-07 01:55
    Ant that is TRWTF. >;->
  • Jeff Grigg 2012-12-07 01:56
    foo:
    leo c:
    can anyone explain it, is there something wrong with the code? I dont get it.
    Hint: There's a "page 1" link below. And surprisingly, this link leads to a full page of previous comments, some of which even related to the current WTF.


    And that is TRWTF.



    (Sorry for the earlier typo! WTF?!? ;-)
  • Jeff Grigg 2012-12-07 01:58
    lunaryorn:
    D-Coder:
    How is it that no one had noticed a problem with discounts less than 10% for so long?

    It seems to be a very generous company... which might explain the quality of the code: There seems to be not enough money left to replace the Office expert dabbling on programming with a real, appropriately paid developer.


    And given that it's been wrong like that for... HOW LONG?!? What makes it such a big crisis *RIGHT NOW*?!?
  • Seahen 2012-12-07 02:20
    [quote user="very punny"][quote] but one thing was for certain - this was going to be a long<!--Get it? Long...Decimal... When was the last time you saw a data type pun?--> week. [\quote]

    He was going to need to use a float-ing holiday after this.
    [/quote]

    If he was union-ized, he could do all the type punning he wanted.
  • ubersoldat 2012-12-07 05:22
    very punny:
    but one thing was for certain - this was going to be a long<!--Get it? Long...Decimal... When was the last time you saw a data type pun?--> week.


    He was going to need to use a float-ing holiday after this.


    There, fixed that for you
  • Stuart Longland 2012-12-07 06:40
    [quote user="very punny"][quote] but one thing was for certain - this was going to be a long<!--Get it? Long...Decimal... When was the last time you saw a data type pun?--> week. [\quote]

    He was going to need to use a float-ing holiday after this.
    [/quote]

    Hmmm, enough to drive me to drink... better make it a double!
  • LonesomeProgrammer 2012-12-07 08:31
    AGray:
    ¯\(°_o)/¯ I DUNNO LOL:
    TRWTF is that some people shouldn't be allowed near anything that requires 9th grade mathematics skills. This isn't even algebra.

    VB? How am I not surprised? (It was either that or PHP.)


    I digress...but, why is it most of the more horrendously stupid WTFs are written in VB? I have never really understood it.


    Smart people know VB is generally not a very good choice of language, so they stay away from it as much as possible. Stupid people don't, so they will program in whatever they can.

    I think I can safely assume that stupid people create more WTFs than smart people, therefore combining these two will result in the proposition that VB code will generally contain more WTFs than code written in a language such as Java or C#.

    Call me arrogant or narrow minded, but the reality really is not much more complicated than that.
  • Nutster 2012-12-07 10:05
    RRDY:
    But dividing by 100 is HARD!

    Yes, dividing is generally takes more time than multiplying, so instead of dividing by 100, multiply by 0.01 instead. Problem solved.
  • gnasher729 2012-12-07 12:05
    [quote user="Nutster"][quote user="RRDY"]But dividing by 100 is HARD![/quote]
    Yes, dividing is generally takes more time than multiplying, so instead of dividing by 100, multiply by 0.01 instead. Problem solved.[/quote

    If that would solve the problem, the compiler would do it for you.

    When you write 0.01, you don't get 0.01 but a floating point number very close to but not exactly equal to 0.01. If you multiply let's say 3700 by that number, the result before rounding isn't 37 but some number close to but not exactly equal to 37. Rounding _may_ round to 37, but isn't guaranteed to. All kinds of funny things may happen as a result.
  • bob 2012-12-07 12:33
    no--you're wrong
  • Kirby L. Wallace 2012-12-07 13:27
    But hey! At least he should get double time overtime pay!
  • Osman 2012-12-07 14:48
    On the double!
  • NotHere 2012-12-07 15:33
    Valetudo:
    Herwig:
    Foo Bar:
    AGray:
    I digress...but, why is it most of the more horrendously stupid WTFs are written in VB? I have never really understood it.

    Possibly because some Peters rise up to their level of incompetence by way of:
    use MS Office products -> write a macro or two -> hey, that's Visual Basic -> VB programmer.

    ...and then they are talking about the "Storage Procedures" they've written in "Virtual Basic" and their other programming skills in in HTML or FTP...


    In the final stages, they talk about writing a gui in VB to trace an IP.


    The WTF in that last statement is that you didn't realize that they purposefully f*cked it up in order to get people to talk about it and watch to try and locate more screw ups.

    Pretty good marketing actually. MS would pay them for mentioning VB; meanwhile it's self serving.
  • Shinobu 2012-12-07 16:34
    A lot of people still don't realise this, but most operations, including division take just one tick. The real performance issues are caused by cache misses and page faults.
    As for the case thing, case-sensitivity is bloody annoying. It says something that IDEs for case-sensitive languages almost always have a way to mitigate it and fix the case.
  • Meep 2012-12-07 18:55
    ¯\(°_o)/¯ I DUNNO LOL:
    TRWTF is that some people shouldn't be allowed near anything that requires 9th grade mathematics skills. This isn't even algebra.

    VB? How am I not surprised? (It was either that or PHP.)


    Being innumerate should be as socially unacceptable as being illiterate.
  • Norman Diamond 2012-12-08 01:25
    C-Derb:
    A discount > 100% would be paying someone to take your products off your hands. At that point, you've got bigger problems than string arithmetic.
    It might be services instead of products. I had a stock transaction where the commission was discounted by 200%. It surprised me, but it looked like it was intentional on the part of the broker because of their rules. Even if that happened more than once I wouldn't get rich on it though.

    It might be something other than services or products too. There have been a few cases where loans had interest rates around -0.005%. Ordinarily one would expect that the lender would be better off just keeping the cash, but someone explained that keepers of cash have to pay to store and guard the cash so the loan was better.
  • Norman Diamond 2012-12-08 01:29
    Meep:
    Being innumerate should be as socially unacceptable as being illiterate.
    In some societies being numerate is socially unacceptable, ranging from nerdishness to offensiveness.

    "Hey look, I had a fantastic game of golf."
    "You added up the numbers wrong. Here's your real score."
    "Well what do you want, a good mathematician or a good golfer?"
    "You're neither."
    "GTFO asshole."
  • old timer 2012-12-08 05:29
    AGray:

    I digress...but, why is it most of the more horrendously stupid WTFs are written in VB? I have never really understood it.


    Because horrendously stupid WTFs in C are uninteligable except to experts, unnoticble except to LINT, and mostly never discovered even when they cause regular crashes.
  • old timer 2012-12-08 05:42
    Tasty:

    I imagine a modern compiler's optimizer will reduce your code to something more efficient. It's not your father's Fortran anymore.


    If it was your father's FORTRAN, it wouldn't need a modern compiler's optimizer to reduce your code to something more efficient.

    Modern copiler optimizers are designed to reduce your C code to something almost as efficient as your Fathers FORTRAN. And even that is possible only if you are using the C99 'restrict' keyword.
  • Norman Diamond 2012-12-09 19:01
    old timer:
    Tasty:

    I imagine a modern compiler's optimizer will reduce your code to something more efficient. It's not your father's Fortran anymore.
    If it was your father's FORTRAN, it wouldn't need a modern compiler's optimizer to reduce your code to something more efficient.
    Yes you would. If I recall correctly, IBM's Fortran G didn't even remove common subexpressions when accessing array elements and didn't do strength reduction in loop variables that traversed an array. Fortran H was designed to be better but it was funny when it wasted CPU time clearing unused portions of a register that were never accessed other than in clearing them.

    The Fortran standard restricted some kinds of expressions in ways that appeared unnecessary. The reason for the restrictions was that compiler writers knew ways to optimize some kinds of expressions, so the executable code could be nearly as fast as assembly. If arbitrary expressions were allowed, the executable code would be 10 times slower than assembly and no one would have switched to Fortran.
  • Mo6eB 2012-12-10 03:46
    He was going to need to use a float-ing holiday after this.

    Oh man, I hope he solves it by converting the string to a float, multiplying by 0.01f and writing the result to string. Next week we'll have a special "I got 5.79999999999999999999999999<snip>9999999999985% discount on this" Error'd edition.

    Personally, I'd take the percentage as a string in base-10, pad on the left with zeroes if needed and insert a '.' two characters before the end.
  • Snowrat 2012-12-10 05:08
    dDiscount←ot×2⊃⎕VFI'0.',¯2↑'0',⍕O.DiscountPercentage

    Fixed it for you
  • Punlicious 2012-12-10 05:50
    You, sir, made my day. I wish more people were as int-elligent as you.
  • Thomas Kolar 2012-12-18 03:05
    [quote]
    [quote] but one thing was for certain - this was going to be a long<!--Get it? Long...Decimal... When was the last time you saw a data type pun?--> week. [\quote]

    He was going to need to use a float-ing holiday after this. [/quote]

    That was an epic... double whammy, if I may say so.
  • Tortoise 2012-12-18 23:00
    ...Fifth grade? If that?