-2

I am trying this script to calculate and print the result in a form input readonly. While this works fine i need the numbers to be shown like this: 2.000.000 instead of 2.000.000.00. I need those 2 zeros at the end gone. I googled it but all I find are solutions with the comma intact. As this script is very basic and simple i hope that for my problem there is a very simple and basic solution.

<script>
        getPrice = function() {
            var numVal1 = Number(document.getElementById("price").value);
            var numVal2 = Number(document.getElementById("discount").value) ;
            var totalValue = numVal1 * numVal2
            document.getElementById("total").value = totalValue.toFixed(2).replace(/\d(?=(\d{3})+\.)/g, '$&.');
        }
    </script>
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Perhaps read the doc https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed – Mark Schultheiss Jan 25 '21 at 22:39
  • Does this answer your question? [How to format a float in javascript?](https://stackoverflow.com/questions/661562/how-to-format-a-float-in-javascript) – Mark Schultheiss Jan 25 '21 at 22:43
  • Does this answer your question? [How to print a number with commas as thousands separators in JavaScript](https://stackoverflow.com/questions/2901102/how-to-print-a-number-with-commas-as-thousands-separators-in-javascript) – tevemadar Jan 25 '21 at 22:49
  • You should assume that JavaScript has ways to format currency in locale-specific ways that don't require you to attempt, and likely get wrong, string manipulation or the use of regex. Example: https://stackoverflow.com/questions/149055/how-to-format-numbers-as-currency-string – jarmod Jan 25 '21 at 22:50

1 Answers1

-1

Have you tried this :

totalValue.toFixed(2).replace(/\d(?=(\d{3})+\.)/g, '$&.').slice(0, -3)
Jurshsmith
  • 182
  • 2
  • 4