-1

im trying to multiply the two value with textbox but im not getting the right answer when they had 2 decimal places. sample output 5.50*5 = 25. i would like to print like this 27.50 instead of 25.

<script>
  if (elm["ItemPrice"].value != "" && elm["ItemQuantity"].value != "") {
    elm["TotalSale"].value = parseInt(elm["ItemPrice"].value) * parseInt(elm["ItemQuantity"].value);
  }
</script>

<script>
  $(document).ready(function () {
    $('multi').DataTable();
  });
</script>
pilchard
  • 12,414
  • 5
  • 11
  • 23
Malandi
  • 41
  • 7
  • Sorry my friend not so familiar in the language – Malandi Mar 27 '21 at 12:13
  • 1
    You're using `parseInt()` which casts the values to integers so any decimal value will be lost. Example: `parseInt(10.5)` will just return `10`. Remove the parseInt() and try with that. – M. Eriksson Mar 27 '21 at 12:18
  • I'm sorry, but I don't see how the added code have anything to do with the question or the above code? – M. Eriksson Mar 27 '21 at 12:19
  • so how i solve this issue? – Malandi Mar 27 '21 at 12:21
  • Test it without using `parseInt()`. You should also post example data/values and show us the expected output and what you're currently getting. Just remember that calculating with floats can give you wrong results: [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – M. Eriksson Mar 27 '21 at 12:21
  • YES! Thank you sir. Big big thank you @MagnusEriksson now is working . How about semi colon? sample 5,500.00 – Malandi Mar 27 '21 at 12:25
  • 2
    I'm sorry but I don't understand what you mean. What semicolon? Do you mean comma `,`? Well `5,500.00` is not a valid number in javascript. you need to change it to `5500.00` for javascript to know what it is. You can do that by simply removing the comma: `elm['ItemPrice'].value.replace(',', '') * elm['ItemQuantity'].value`. – M. Eriksson Mar 27 '21 at 12:31

1 Answers1

0

This would work. You are converting the value in Int and the value is in Float

<script>
  if (elm["ItemPrice"].value != "" && elm["ItemQuantity"].value != "") {
    elm["TotalSale"].value = parseFloat(elm["ItemPrice"].value) * elm["ItemQuantity"].value;
  }
</script>

<script>
  $(document).ready(function () {
    $('multi').DataTable();
  });
</script>
  • 1
    I don't think the "ItemQuantity" should be cast as a float. Quantity tends to be integers. – M. Eriksson Mar 27 '21 at 12:32
  • 1
    @MagnusEriksson Well to be fair sometimes I order a Pint of Guiness and a half of Bitter.... So I have something to drink while the Guiness is settling:) – RiggsFolly Mar 27 '21 at 12:33
  • without parseInt its working very perfectly. but, im getting the 55.33 * 303 = 16764.989999999998. parseFloat will solve my issue? – Malandi Mar 27 '21 at 12:42
  • @RiggsFolly - Fair enough. I should have specified _"unless Guiness is involved_ :-) – M. Eriksson Mar 27 '21 at 12:42
  • @MagnusEriksson hi sir do you know how to print semi colon the textbox? bytheway thank you. i would like to see the output like this 5,5000.55 – Malandi Mar 27 '21 at 12:49
  • @Malandi - It's not a semi colon, it's a comma. Try using the [Intl.NumberFormat()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat) function. I don't write much JS myself these days so that was just want I found on a quick search. You need to read that page and see if it would work and how to use it. – M. Eriksson Mar 27 '21 at 12:52
  • @MagnusEriksson thank you but idk how to use that. i tried but is not working. – Malandi Mar 27 '21 at 13:13
  • @Malandi - Then you need to do more testing/research. Since the original question has been resolved, you need to post another question if you have more issues you need help with. However, you need to do some extensive research and make some attempts yourself before asking though. – M. Eriksson Mar 27 '21 at 13:15