Created
September 18, 2017 16:22
-
-
Save codenamejason/4a8484e0e8f291487cc8d9da2079c202 to your computer and use it in GitHub Desktop.
Converts number to money for knockout.js
This file contains hidden or 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
ko.bindingHandlers.numericText = { | |
update: function(element, valueAccessor, allBindingsAccessor) { | |
var value = ko.utils.unwrapObservable(valueAccessor()); | |
var precision = ko.utils.unwrapObservable(allBindingsAccessor().precision) || | |
ko.bindingHandlers.numericText.defaultPrecision; | |
if (value === undefined || value === null) { | |
return; | |
} | |
var formattedValue = Number(value).toFixed(precision); | |
ko.bindingHandlers.text.update(element, | |
function() { | |
return "$" + formattedValue; | |
}); | |
}, | |
// Default of 2 decimal places** | |
defaultPrecision: 2 | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment