CodeSOD: Parsimony
When I was but a wee lass, the internet was still a wild, untamed place. Before the advent of walled gardens and minifiers, long before Facebook, you could learn everything you needed to know about web programming using one simple tool: View -> Source.
Wide open fields of HTML, speckled with poignant blossoms of inline CSS! Table after table, each one hand-crafted and oddly indented! Gifs ripe for stealing: dancing flames, dancing babies, and 'under construction' signs! What a wonderful world of secrets lay hidden just two clicks away!
Sometimes, in rare occasions, you can still see whole, unmolested Javascript functions in the source code of pages. Today's function comes from a credit union, ready and waiting to teach a new generation of programmers all about number parsing.
function formatNum(Vnum) { if (Vnum > 99000000) { alert("Sorry, this will not generate numbers larger that 99 million."); focus(); } else { var V10million = parseInt(Vnum / 10000000); var V1million = (Vnum % 10000000) / 1000000; if (V1million / 1000000 == 1) { V1million = 1; } else if (V1million = 10000000) { Vformat = (V10million + "" + V1million + "," + V100thousand + "" + V10thousand + "" + V1thousand + "," + Vhundreds + "" + Vtens + "" + Vones + "." + Vcents); } else if (Vnum >= 1000000) { Vformat = (V1million + "," + V100thousand + "" + V10thousand + "" + V1thousand + "," + Vhundreds + "" + Vtens + "" + Vones + "." + Vcents); } else if (Vnum >= 100000) { Vformat = (V100thousand + "" + V10thousand + "" + V1thousand + "," + Vhundreds + "" + Vtens + "" + Vones + "." + Vcents); } else if (Vnum >= 10000) { Vformat = (V10thousand + "" + V1thousand + "," + Vhundreds + "" + Vtens + "" + Vones + "." + Vcents); } else if (Vnum >= 1000) { Vformat = (V1thousand + "," + Vhundreds + "" + Vtens + "" + Vones + "." + Vcents); } else if (Vnum >= 100) { Vformat = (Vhundreds + "" + Vtens + "" + Vones + "." + Vcents); } else if (Vnum >= 10) { Vformat = (Vtens + "" + Vones + "." + Vcents); } else if (Vnum >= 1) { Vformat = (Vones + "." + Vcents); } else { Vformat = ("0." + Vcents); } return Vformat; }}
Inputting "8343.33" to this function results in the output of "8,343.32". Why? It's a feature. Ship it!
[Advertisement] Incrementally adopt DevOps best practices with BuildMaster, ProGet and Otter, creating a robust, secure, scalable, and reliable DevOps toolchain.