0

Guys I got a text box wherein users may enter valid decimal numbers.

And later I have to do calculations with this decimal number without rounding it.

Currently I am working as

 var pricePerItemExVat = Math.round($("#InnerCaseWsp").val()) / Math.round($("#InnerCaseItemSellUnit").val());

How do I do the same without rounding the number ?

Nishant Rambhojun
  • 77
  • 1
  • 2
  • 10

1 Answers1

3

It looks like you used Math.round to get numbers from strings.

Just parse the strings to numbers using parseFloat :

var pricePerItemExVat = parseFloat($("#InnerCaseWsp").val()) / parseFloat($("#InnerCaseItemSellUnit").val());
Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
  • Actually `parseFloat` isn't even necessary because `/` will automatically cast it into a number (as defined in the spec.) – Derek 朕會功夫 Apr 22 '14 at 05:47
  • @Derek朕會功夫 In **that specific case** yes, but OP says he has other computations and I guess some of them involve other operators, for example `+`. – Denys Séguret Apr 22 '14 at 05:49
  • 1
    Well if he does have `+` in his other code then I believe `parseFloat` is indeed needed... or just a `+` in front would also do it. – Derek 朕會功夫 Apr 22 '14 at 05:52