Naga Phaneendra
Naga Phaneendra

Reputation: 53

How to calculate point values in javascript?

I have written code to calculate the values like integer values.

function add_number(inp) {
    var tr = inp.parentNode.parentNode;
    var first_number = parseInt(tr.querySelector('[data-id="qty"]').value);
    var second_number = parseInt(tr.querySelector('[data-id="amtper"]').value);

    var result = first_number * second_number;
    tr.querySelector('[data-id="total"]').value = result ? result : '';
}

By the above code U am able to calculate only round values, if I calculate the values 10.60*10 = 106, (the value should be displayed). But I am getting 100.

How to calculate point values?

Upvotes: 1

Views: 505

Answers (4)

Doctor Strange
Doctor Strange

Reputation: 351

parseInt will convert 10.60 to 10 before the multiplication. You need to use parseFloat instead. In case you expect an integer or a number with fixed number digits after the decimal point, you can use the precision parameter to set it.

function add_number(inp, precision) {
    precision = typeof precision !== "number"? 0: precision;
    var tr = inp.parentNode.parentNode;
    var first_number = parseFloat(tr.querySelector('[data-id="qty"]').value);
    var second_number = parseFloat(tr.querySelector('[data-id="amtper"]').value);

    var result = (first_number * second_number).toFixed(precision);
    tr.querySelector('[data-id="total"]').value = result ? result : '';
}

Upvotes: 0

Jeroen
Jeroen

Reputation: 63810

Use parseFloat? See this:

function add_number(inp) {
    var tr = inp.parentNode.parentNode;
    var first_number = parseFloat(tr.querySelector('[data-id="qty"]').value);
    var second_number = parseFloat(tr.querySelector('[data-id="amtper"]').value);

    var result = first_number * second_number;
    tr.querySelector('[data-id="total"]').value = result ? result : '';
}

add_number(document.getElementById("inp"));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div>
  QTY: <input data-id="qty" value="100"><br>
  AMTPER: <input data-id="amtper" value="1.06">
  <div>
    <div id="inp"></div>
  </div>
  <hr>
  Total, filled by JS:
  <input data-id="total">
</div>

Upvotes: 0

gurvinder372
gurvinder372

Reputation: 68433

if I calculate the values 10.60*10 = 106, (the value should be displayed). But I am getting 100

Because when you do parseInt, it returns an Intger without any decimal value.

Use parseFloat instead, Replace

var first_number = parseInt(tr.querySelector('[data-id="qty"]').value);
var second_number = parseInt(tr.querySelector('[data-id="amtper"]').value);

with

var first_number = parseFloat(tr.querySelector('[data-id="qty"]').value);
var second_number = parseFloat(tr.querySelector('[data-id="amtper"]').value);

Upvotes: 1

Munawir
Munawir

Reputation: 3356

parseInt() parses only integer values without decimal value.

Use parseFloat() instead.

var first_number = parseFloat(tr.querySelector('[data-id="qty"]').value);
var second_number = parseFloat(tr.querySelector('[data-id="amtper"]').value);

Upvotes: 0

Related Questions