This is an example for the blog post 'KnockoutJS for XAML Developers' - http://www.rahulpnath.com/blog/knockoutjs-for-xaml-developers/
A Pen by Rahul P Nath on CodePen.
| Enter a value and tab off and you will see the currency $ coming in automatically. | |
| <p>Enter bid price: <input data-bind="value: formattedPrice"/></p> |
This is an example for the blog post 'KnockoutJS for XAML Developers' - http://www.rahulpnath.com/blog/knockoutjs-for-xaml-developers/
A Pen by Rahul P Nath on CodePen.
| function MyViewModel() { | |
| this.price = ko.observable(25.99); | |
| this.formattedPrice = ko.computed({ | |
| // Similar to Convert function on IValueConverter | |
| read: function () { | |
| return '$' + this.price().toFixed(2); | |
| }, | |
| // Similar to ConvertBack on IValueConverter | |
| write: function (value) { | |
| // Strip out unwanted characters, parse as float, then write the raw data back to the underlying "price" observable | |
| value = parseFloat(value.replace(/[^\.\d]/g, "")); | |
| this.price(isNaN(value) ? 0 : value); // Write to underlying storage | |
| }, | |
| owner: this | |
| }); | |
| } | |
| ko.applyBindings(new MyViewModel()); |