Close Menu
    DevStackTipsDevStackTips
    • Home
    • News & Updates
      1. Tech & Work
      2. View All

      GitHub’s CEO Thomas Dohmke steps down, triggering tighter integration of company within Microsoft

      August 12, 2025

      bitHuman launches SDK for creating AI avatars

      August 12, 2025

      Designing With AI, Not Around It: Practical Advanced Techniques For Product Design Use Cases

      August 11, 2025

      Why Companies Are Investing in AI-Powered React.js Development Services in 2025

      August 11, 2025

      I found a Google Maps alternative that won’t track you or drain your battery – and it’s free

      August 12, 2025

      I tested this new AI podcast tool to see if it can beat NotebookLM – here’s how it did

      August 12, 2025

      Microsoft’s new update makes your taskbar a productivity hub – here’s how

      August 12, 2025

      Save $50 on the OnePlus Pad 3 plus get a free gift – here’s the deal

      August 12, 2025
    • Development
      1. Algorithms & Data Structures
      2. Artificial Intelligence
      3. Back-End Development
      4. Databases
      5. Front-End Development
      6. Libraries & Frameworks
      7. Machine Learning
      8. Security
      9. Software Engineering
      10. Tools & IDEs
      11. Web Design
      12. Web Development
      13. Web Security
      14. Programming Languages
        • PHP
        • JavaScript
      Featured

      Laravel Global Scopes: Automatic Query Filtering

      August 12, 2025
      Recent

      Laravel Global Scopes: Automatic Query Filtering

      August 12, 2025

      Building MCP Servers in PHP

      August 12, 2025

      Filament v4 is Stable!

      August 12, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured

      I Asked OpenAI’s New Open-Source AI Model to Complete a Children’s School Test — Is It Smarter Than a 10-Year-Old?

      August 12, 2025
      Recent

      I Asked OpenAI’s New Open-Source AI Model to Complete a Children’s School Test — Is It Smarter Than a 10-Year-Old?

      August 12, 2025

      Madden NFL 26 Leads This Week’s Xbox Drops—But Don’t Miss These Hidden Gems

      August 12, 2025

      ASUS G14 Bulked Up for 2025—Still Sexy, Just a Bit Chonkier

      August 12, 2025
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»News & Updates»CodeSOD: Round Strips

    CodeSOD: Round Strips

    August 12, 2025

    JavaScript is frequently surprising in terms of what functions it does not support. For example, while it has a Math.round function, that only rounds to the nearest integer, not an arbitrary precision. That’s no big deal, of course, as if you wanted to round to, say, four decimal places, you could write something like: Math.floor(n * 10000) / 10000.

    But in the absence of a built-in function to handle that means that many developers choose to reinvent the wheel. Ryan found this one.

    function stripExtraNumbers(num) {
        //check if the number's already okay
        //assume a whole number is valid
        var n2 = num.toString();
        if(n2.indexOf(".") == -1)  { return num; }
        //if it has numbers after the decimal point,
        //limit the number of digits after the decimal point to 4
        //we use parseFloat if strings are passed into the method
        if(typeof num == "string"){
            num = parseFloat(num).toFixed(4);
        } else {
            num = num.toFixed(4);
        }
        //strip any extra zeros
        return parseFloat(num.toString().replace(/0*$/,""));
    }
    

    We start by turning the number into a string and checking for a decimal point. If it doesn’t have one, we’ve already rounded off, return the input. Now, we don’t trust our input, so if the input was already a string, we’ll parse it into a number. Once we know it’s a number, we can call toFixed, which returns a string rounded off to the correct number of decimal points.

    This is all very dumb. Just dumb. But it’s the last line which gets really dumb.

    toFixed returns a padded string, e.g. (10).toFixed(4) returns "10.0000". But this function doesn’t want those trailing zeros, so they convert our string num into a string, then use a regex to replace all of the trailing zeros, and then parse it back into a float.

    Which, of course, when storing the number as a number, we don’t really care about trailing zeros. That’s a formatting choice when we output it.

    I’m always impressed by a code sample where every single line is wrong. It’s like a little treat. In this case, it even gets me a sense of how it evolved from little snippets of misunderstood code. The regex to remove trailing zeros in some other place in this developer’s experience led to degenerate cases where they had output like 10., so they also knew they needed to have the check at the top to see if the input had a fractional part. Which the only way they knew to do that was by looking for a . in a string (have fun internationalizing that!). They also clearly don’t have a good grasp on types, so it makes sense that they have the extra string check, just to be on the safe side (though it’s worth noting that parseFloat is perfectly happy to run on a value that’s already a float).

    This all could be a one-liner, or maybe two if you really need to verify your types. Yet here we are, with a delightfully wrong way to do everything.

    [Advertisement]
    Keep the plebs out of prod. Restrict NuGet feed privileges with ProGet. Learn more.

    Source: Read More 

    Facebook Twitter Reddit Email Copy Link
    Previous ArticleLaravel’s UsePolicy Attribute: Explicit Authorization Control
    Next Article gita – manage multiple git repos

    Related Posts

    News & Updates

    I found a Google Maps alternative that won’t track you or drain your battery – and it’s free

    August 12, 2025
    News & Updates

    I tested this new AI podcast tool to see if it can beat NotebookLM – here’s how it did

    August 12, 2025
    Leave A Reply Cancel Reply

    For security, use of Google's reCAPTCHA service is required which is subject to the Google Privacy Policy and Terms of Use.

    Continue Reading

    8 Best Free and Open Source Terminal-Based Flashcard Tools

    Linux

    Microsoft kills Movies & TV storefront on Windows and Xbox — here’s what will happen to your purchased media

    News & Updates

    WasIstLos – unofficial WhatsApp desktop application

    Linux

    CVE-2025-4798 – WordPress WP-DownloadManager Arbitrary File Read Vulnerability

    Common Vulnerabilities and Exposures (CVEs)

    Highlights

    News & Updates

    Windows 11’s new “App Actions” feature will let apps contextually present themselves in the OS

    May 19, 2025

    Microsoft is giving developers access to an API that will enable specific features to appear…

    CVE-2025-47154 – Ladybird LibJS Use-After-Free Remote Code Execution Vulnerability

    May 1, 2025

    Hurry Curry! – multiplayer co-op coooking game

    July 23, 2025

    Designing for Touch: How Finger-Friendly UI Enhances UX

    July 28, 2025
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.