So, it sounds like you are using the mapping plugin (ko.mapping.fromJSON) to turn your JSON from the server into your viewModel, right? Are you sure that the numbers in your JSON are not wrapped in quotes? If they are numbers, then it looks like the mapping plugin will keep them as numbers.
Otherwise, is this happening after somebody edits an input field? If you have a number in your view model, bind it to an input field, then edit that input field, it will become a string.
One way to prevent that is to bind your edit field to a writable dependentObservable that will make sure that it is a number when writing back to the real observable.
So, you could bind to "editValue" here:
viewModel.realValue = ko.observable(10);
viewModel.editValue = ko.dependentObservable({
read: function() {
return this.realValue();
},
write: function(newValue) {
var parsedValue = parseFloat(newValue);
this.realValue(isNaN(parsedValue) ? newValue : parsedValue);
},
owner: viewModel
});
or you can create a special observable that forces the value to be numeric like:
ko.numericObservable = function(initialValue) {
var _actual = ko.observable(initialValue);
var result = ko.dependentObservable({
read: function() {
return _actual();
},
write: function(newValue) {
var parsedValue = parseFloat(newValue);
_actual(isNaN(parsedValue) ? newValue : parsedValue);
}
});
return result;
};
Now, you can use this like any observable:
viewModel.realValue = ko.numericObservable(10);
I might have slightly misinterpreted your question, if your numeric values are not bound to input fields. Let me know if you are talking about a different scenario.