Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. The Weird and The Wonderful
  4. A Brilliant Replacement of float.toFixed(2)

A Brilliant Replacement of float.toFixed(2)

Scheduled Pinned Locked Moved The Weird and The Wonderful
javascriptrubyquestion
8 Posts 6 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    TNCaver
    wrote on last edited by
    #1

    Our finance folks were a bit annoyed by one of our web pages, where it occasionally rounded the final total to the nearest dime. While troubleshooting a former colleague's JavaScript code that added 2% to the amount entered by the user, I found this gem. His intention, at least, was obvious by the function name.

    function CurrencyFormatted(amount) {
    var i = parseFloat(amount);
    if (isNaN(i)) { i = 0.00; }
    var minus = '';
    if (i < 0) { minus = '-'; }
    i = Math.abs(i);
    i = parseInt((i + .005) * 100);
    i = i / 100;
    s = new String(i);
    if (s.indexOf('.') < 0) { s += '.00'; }
    if (s.indexOf('.') == (s.length - 2)) { s += '0'; }
    s = minus + s;
    return s;
    }

    Isn't that brilliant? I mean, why use the simple, single-line solution isFixed(2) when you can do the same thing in 9 lines? :wtf:

    If you think 'goto' is evil, try writing an Assembly program without JMP.

    L M B 3 Replies Last reply
    0
    • T TNCaver

      Our finance folks were a bit annoyed by one of our web pages, where it occasionally rounded the final total to the nearest dime. While troubleshooting a former colleague's JavaScript code that added 2% to the amount entered by the user, I found this gem. His intention, at least, was obvious by the function name.

      function CurrencyFormatted(amount) {
      var i = parseFloat(amount);
      if (isNaN(i)) { i = 0.00; }
      var minus = '';
      if (i < 0) { minus = '-'; }
      i = Math.abs(i);
      i = parseInt((i + .005) * 100);
      i = i / 100;
      s = new String(i);
      if (s.indexOf('.') < 0) { s += '.00'; }
      if (s.indexOf('.') == (s.length - 2)) { s += '0'; }
      s = minus + s;
      return s;
      }

      Isn't that brilliant? I mean, why use the simple, single-line solution isFixed(2) when you can do the same thing in 9 lines? :wtf:

      If you think 'goto' is evil, try writing an Assembly program without JMP.

      L Offline
      L Offline
      LloydA111
      wrote on last edited by
      #2

      Perhaps he didn't know isFixed exists

             .-.
            |o,o|
         ,| \_\\=/\_      .-""-.
         ||/\_/\_\\\_\\    /\[\] \_ \_\\
         |\_/|(\_)|\\\\  \_|\_o\_LII|\_
            \\.\_./// / | ==== | \\
            |\\\_/|"\` |\_| ==== |\_|
            |\_|\_|    ||" ||  ||
            |-|-|    ||LI  o ||
            |\_|\_|    ||'----'||
           /\_/ \\\_\\  /\_\_|    |\_\_\\
      
      1 Reply Last reply
      0
      • T TNCaver

        Our finance folks were a bit annoyed by one of our web pages, where it occasionally rounded the final total to the nearest dime. While troubleshooting a former colleague's JavaScript code that added 2% to the amount entered by the user, I found this gem. His intention, at least, was obvious by the function name.

        function CurrencyFormatted(amount) {
        var i = parseFloat(amount);
        if (isNaN(i)) { i = 0.00; }
        var minus = '';
        if (i < 0) { minus = '-'; }
        i = Math.abs(i);
        i = parseInt((i + .005) * 100);
        i = i / 100;
        s = new String(i);
        if (s.indexOf('.') < 0) { s += '.00'; }
        if (s.indexOf('.') == (s.length - 2)) { s += '0'; }
        s = minus + s;
        return s;
        }

        Isn't that brilliant? I mean, why use the simple, single-line solution isFixed(2) when you can do the same thing in 9 lines? :wtf:

        If you think 'goto' is evil, try writing an Assembly program without JMP.

        M Offline
        M Offline
        Marc Clifton
        wrote on last edited by
        #3

        Well, perhaps toFixed doesn't work the way he wanted? Does it handle NaN? Does it do rounding? Does it round the absolute value of the value (which would always round up, right?) Goofing around with the W3School's Try It[^] feature (snazzy) I think everything works correctly except the NaN handling. Marc

        Testers Wanted!
        Latest Article: User Authentication on Ruby on Rails - the definitive how to
        My Blog

        A T 2 Replies Last reply
        0
        • M Marc Clifton

          Well, perhaps toFixed doesn't work the way he wanted? Does it handle NaN? Does it do rounding? Does it round the absolute value of the value (which would always round up, right?) Goofing around with the W3School's Try It[^] feature (snazzy) I think everything works correctly except the NaN handling. Marc

          Testers Wanted!
          Latest Article: User Authentication on Ruby on Rails - the definitive how to
          My Blog

          A Offline
          A Offline
          AlphaDeltaTheta
          wrote on last edited by
          #4

          Marc Clifton wrote:

          Does it handle NaN?

          I don't think NaN will ever arise while currency formatting (going as per method name), without exceptions. He's not doing tan 90 anywhere is he?

          B 1 Reply Last reply
          0
          • A AlphaDeltaTheta

            Marc Clifton wrote:

            Does it handle NaN?

            I don't think NaN will ever arise while currency formatting (going as per method name), without exceptions. He's not doing tan 90 anywhere is he?

            B Offline
            B Offline
            Brisingr Aerowing
            wrote on last edited by
            #5

            You have a good point there. :thumbsup:

            Gryphons Are Awesome! ‮Gryphons Are Awesome!‬

            1 Reply Last reply
            0
            • T TNCaver

              Our finance folks were a bit annoyed by one of our web pages, where it occasionally rounded the final total to the nearest dime. While troubleshooting a former colleague's JavaScript code that added 2% to the amount entered by the user, I found this gem. His intention, at least, was obvious by the function name.

              function CurrencyFormatted(amount) {
              var i = parseFloat(amount);
              if (isNaN(i)) { i = 0.00; }
              var minus = '';
              if (i < 0) { minus = '-'; }
              i = Math.abs(i);
              i = parseInt((i + .005) * 100);
              i = i / 100;
              s = new String(i);
              if (s.indexOf('.') < 0) { s += '.00'; }
              if (s.indexOf('.') == (s.length - 2)) { s += '0'; }
              s = minus + s;
              return s;
              }

              Isn't that brilliant? I mean, why use the simple, single-line solution isFixed(2) when you can do the same thing in 9 lines? :wtf:

              If you think 'goto' is evil, try writing an Assembly program without JMP.

              B Offline
              B Offline
              Bob1000
              wrote on last edited by
              #6

              Presume they are paid by the line!

              T 1 Reply Last reply
              0
              • M Marc Clifton

                Well, perhaps toFixed doesn't work the way he wanted? Does it handle NaN? Does it do rounding? Does it round the absolute value of the value (which would always round up, right?) Goofing around with the W3School's Try It[^] feature (snazzy) I think everything works correctly except the NaN handling. Marc

                Testers Wanted!
                Latest Article: User Authentication on Ruby on Rails - the definitive how to
                My Blog

                T Offline
                T Offline
                TNCaver
                wrote on last edited by
                #7

                The second line handles NaN. How extensive were your tests in W3Schools Try It? The code in question ran in production for a few years, and it runs flawlessly 99.99% of the time. But that 0.01% where it didn't is why I was looking into it. My point is this: the amount of time needed to write and test that convoluted mass of spaghetti, and to understand and troubleshoot it later, is unacceptable when a simple solution is built-in and readily available. If he didn't know about toFixed() is not much of an excuse; one should know your tools, or at least know how Google works. You still need a solution that catches NaN, so it makes sense to wrap isFixed in a function:

                function CurrencyFormatted(amount) {
                var i = parseFloat(amount);
                if (isNaN(i)) { i = 0.00; }
                return i.toFixed(2);
                }

                If you think 'goto' is evil, try writing an Assembly program without JMP.

                1 Reply Last reply
                0
                • B Bob1000

                  Presume they are paid by the line!

                  T Offline
                  T Offline
                  TNCaver
                  wrote on last edited by
                  #8

                  :laugh: My exact thoughts when I saw that. But no, much as I'd like it, we're salaried here. This fellow doesn't work here any more, and as I've gone back to maintain his code I can see that we should kick ourselves in the arse for not holding code reviews when he was here. I could teach a full semester class on Programming Best Practices and use only his code for examples of what not to do. Not that my code is perfect, but ...

                  If you think 'goto' is evil, try writing an Assembly program without JMP.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups