-2
var amount = vm.AMOUNT();

The Above statement is coming with decimal point, I need to convert this statement to Interger.(Without Decimal Point)

Jamiec
  • 133,658
  • 13
  • 134
  • 193
Avi
  • 35
  • 1
  • 9
  • Possible duplicate of [How do I convert a float number to a whole number in JavaScript?](http://stackoverflow.com/questions/596467/how-do-i-convert-a-float-number-to-a-whole-number-in-javascript) – Jamiec May 19 '16 at 09:30
  • You could use the method provided in the previous link inside a _knockout extender_: http://knockoutjs.com/documentation/extenders.html Check out "Live Example 1" – user3297291 May 19 '16 at 10:42
  • Jamiec - I am asking this in knockout not in Jquery or Javascript. – Avi May 19 '16 at 12:44
  • @user3297291 it doesn't say about float to interger. i have already seen that website and i have tried too. it is not working – Avi May 19 '16 at 12:46
  • 1
    You're using Knockout so you're using Javascript. The link provided by @Jamiec provides a solution. If you don't feel that will work for you, please show example code of what isn't working and [what you've already tried](http://stackoverflow.com/help/mcve). – PatrickSteele May 19 '16 at 13:00
  • Knockout *is* javascript. – Jamiec May 19 '16 at 13:32

1 Answers1

4

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
Community
  • 1
  • 1
user3297291
  • 22,592
  • 4
  • 29
  • 45