Created
December 6, 2017 15:31
-
-
Save codenamejason/72486bca48b8f1b9d653e7eba0aee244 to your computer and use it in GitHub Desktop.
Custom binding for displaying money values
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
// binding for displaying money values | |
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