-4

I have an input field which accepts only integer. And I want to maintain the integer always with two decimal points like 10.00 or 4.00 .

I have already tried toFixed() and toPrecise() already both converts the integer to string.

var anum = 134;

console.log(
  anum.toFixed(2), //"134.00"
  typeof(anum.toFixed(2)) //"string"
)

I want to maintain it as integer only and not string

adiga
  • 34,372
  • 9
  • 61
  • 83
Balaji G
  • 11
  • 1

1 Answers1

1

Numbers, both in JavaScript and in the abstract, don't have a fixed number of decimal places. (Some kinds of computer numbers do, like Java's BigDecimal, but not the ones in JavaScript.) When you convert the input string to a number, the concept of the number of decimal places that were on the string disappears. If you want to remember that so you can use it when converting back to string, you'll need to store that information (from the input string) separately.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875