Working with Dollars & Cents in JavaScript: The Definitive Guide
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
console.log("****************************************************"); | |
console.log("**** Working With Dollars & Cents in JavaScript ****"); | |
console.log("**** ~ The Definitive Guide ~ ****"); | |
console.log("****************************************************"); | |
console.log('\n'); | |
const formatAsDollarsAndCents = (value) => { | |
var newValue = parseFloat(value); | |
newValue = newValue.toFixed(2); | |
return newValue; | |
} | |
// Floating Point Numbers Formatted to 2 Decimal Places | |
console.log("Floating Point Numbers Formatted to 2 Decimal Places"); | |
const nonStandardAmountFormatted = formatAsDollarsAndCents(34.089999999999996); | |
console.log(nonStandardAmountFormatted); | |
const oneDecimalPlaceFormatted = formatAsDollarsAndCents(1.1); | |
console.log(oneDecimalPlaceFormatted); | |
const wholeNumberFormatted = formatAsDollarsAndCents(1); | |
console.log(wholeNumberFormatted); | |
console.log('\n'); | |
// Strings Formatted to 2 Decimal Places | |
console.log("Cast As String..."); | |
const nonStandardAmountFormattedAsString = nonStandardAmountFormatted.toString(); | |
console.log('$' + nonStandardAmountFormattedAsString); | |
const oneDecimalPlaceFormattedAsString = oneDecimalPlaceFormatted.toString(); | |
console.log('$' + oneDecimalPlaceFormattedAsString); | |
const wholeNumberFormattedAsString = wholeNumberFormatted.toString(); | |
console.log('$' + wholeNumberFormattedAsString); | |
console.log('\n'); | |
// Need to add commas? Here's the regex magic sprinkled in... | |
const myNumber = 1300000.89 | |
const myNumberAsString = myNumber.toString(); | |
const myNumberWithCommas = myNumberAsString.replace(/\d(?=(\d{3})+\.)/g, '$&,'); | |
console.log('Add commas... '); | |
console.log('$' + myNumberWithCommas); | |
// Edge case, need to add commas to a whole number? I've written a function that | |
// will add commas to a string of numeric characters. This is helpful when working | |
// with integers, as the regular expression above does not work with whole numbers. | |
// (One could write a regular expression that works with whole numbers, but I'm not | |
// very good with regex so I just wrote a function and moved on with my life ;) | |
function getWholeNumberWithCommas(wholeNumberStr) { | |
// Step One, Reverse String | |
var reversedString = ""; | |
var indexOfLastChar = wholeNumberStr.length - 1; | |
for (var i = indexOfLastChar; i >= 0; i--) { | |
reversedString = reversedString + wholeNumberStr[i]; | |
} | |
// Step Two, Add Commas Every Third Char | |
var reversedStringWithCommas = ""; | |
for (var i = 0; i < reversedString.length; i++) { | |
// every third char, add a comma and the next char | |
if (i % 3 === 0 && i !== 0) { | |
reversedStringWithCommas = reversedStringWithCommas + "," + reversedString[i]; | |
} else { | |
reversedStringWithCommas = reversedStringWithCommas + reversedString[i]; | |
} | |
} | |
// Step Three, Reverse reversedStringWithCommas, and we'll have | |
// the whole number with commas that we want | |
var wholeNumberWithCommas = ""; | |
indexOfLastChar = reversedStringWithCommas.length - 1; | |
for (var i = indexOfLastChar; i >= 0; i--) { | |
wholeNumberWithCommas = wholeNumberWithCommas + reversedStringWithCommas[i]; | |
} | |
// Step Four, return wholeNumberWithCommas | |
return wholeNumberWithCommas; | |
} | |
console.log('\n'); | |
var testNumber = "9283749823749872398472938742983748"; | |
var testNumberWithCommas = getWholeNumberWithCommas(testNumber); | |
console.log('Add commas to a whole number (represented as a string)'); | |
console.log(testNumberWithCommas); | |
console.log('\n'); | |
console.log("****************************************************"); | |
console.log("****************************************************"); | |
console.log("****************************************************"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment