var amount = vm.AMOUNT();
The Above statement is coming with decimal point, I need to convert this statement to Interger.(Without Decimal Point)
var amount = vm.AMOUNT();
The Above statement is coming with decimal point, I need to convert this statement to Interger.(Without Decimal Point)
From your comments I get the impression that linking to previous answers to similar questions might not help you out. So, for the sake of clarity:
Original question:
I need to convert this statement to Interger [sic]
Javascript doesn't have a distinction between integers and floats; there's only Number
(read more on MDN)
There are many ways to remove a decimal from a number. E.g.: you can use Math.round()
, Math.ceil()
or Math.floor()
(if you want to go from n to 0 decimals).
var amount = vm.AMOUNT();
var roundedAmount = Math.round(amount); // As close as you'll get to an 'Integer'
Comment #1:
I am asking this in knockout not in Jquery or Javascript
jQuery and knockout are javascript libraries. Functions such as Math.round
are in the javascript spec and can be used no matter what libraries you're including.
Comment #2:
You could use the method provided in the previous link inside a knockout extender: knockoutjs.com/documentation/extenders.html Check out "Live Example 1" – me
it doesn't say about float to interger – you
I was trying to point you towards a solution that matches a way of coding that works well with knockout.
var amount = vm.AMOUNT();
To me, it seems that vm.AMOUNT
is an observable that is set with values you don't want (i.e.: there's too many decimals).
If you want to control the types of values that your observable can contain, you can use an extender
. This enables you to convert any value that is used to set AMOUNT
's value. If you do need both the decimal value as well as the rounded value, here's another approach:
var roundedAmount = ko.computed(function() {
return Math.round(vm.AMOUNT());
});
vm.AMOUNT(12.34);
console.log(roundedAmount()); // Logs: 12