Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Created June 14, 2023 05:15
Show Gist options
  • Save code-boxx/600b5ea572e17ef296850ad33e03a644 to your computer and use it in GitHub Desktop.
Save code-boxx/600b5ea572e17ef296850ad33e03a644 to your computer and use it in GitHub Desktop.
Javascript Pad Number With Zeroes

JAVASCRIPT ADD LEADING & TRAILING ZEROES

https://code-boxx.com/add-leading-trailing-zeroes-javascript/

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

<!DOCTYPE html>
<html>
<head>
<title>Easy Zeroes</title>
<script>
// (A) THE NUMBERS
var numA = 123;
var numB = 123.45;
// (B) STRING CONCAT
console.log("000" + numA);
console.log(numB + "000");
// (C) PAD STRING
console.log(numA.toString().padStart(5, 0));
console.log(numB.toString().padEnd(8, 0));
</script>
</head>
<body></body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Add Leading & Trailing Zeroes</title>
<script>
// (A) PAD WITH ZEROES
// NUM : THE ORIGINAL NUMBER
// LEAD : TOTAL NUMBER OF DIGITALS ALLOWED (WHOLE NUMBERS)
// TRAIL : TOTAL NUMBER OF DIGITALS ALLOWED (DECIMAL NUMBERS)
function padZero (num, lead, trail) {
// (A1) CONVERT NUMBER TO STRING
var cString = num.toString();
// (A2) GET NUMBER OF DIGITS
var cWhole, cDec, cCheck = cString.indexOf(".");
// NO DECIMAL PLACES, A WHOLE NUMBER
if (cCheck == -1) {
cWhole = cString.length;
cDec = 0;
cString += ".";
}
// IS A DECIMAL NUMBER
else {
cWhole = cCheck;
cDec = cString.substr(cCheck+1).length;
}
// (A3) PAD WITH LEADING ZEROES
if (cWhole < lead) {
for (let i=cWhole; i<lead; i++) { cString = "0" + cString; }
}
// (A4) PAD WITH TRAILING ZEROES
if (cDec < trail) {
for (let i=cDec; i<trail; i++) { cString += "0"; }
}
return cString;
}
// (B) TEST
// (B1) WHOLE NUMBER
var first = padZero(123, 5, 3);
console.log(first); // 00123.000
// (B2) DECIMAL
var second = padZero(123.45, 5, 3);
console.log(second); // 00123.450
</script>
</head>
<body></body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Remove Zero</title>
<script>
// (A) PARSE FLOAT & INT
console.log(parseFloat("0123.4500")); // 123.45
console.log(parseInt("0123.4500")); //123
// (B) USING REGULAR EXPRESSION
// Credits: https://stackoverflow.com/questions/594325/input-field-value-remove-leading-zeros
function removeZero (num) {
return num.replace(/^0+(?!\.|$)/, "");
}
console.log(removeZero("000.00")); // 0.0
console.log(removeZero("00000")); // 0
console.log(removeZero("012.30")); // 12.30
</script>
</head>
<body></body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment