Space Age Crystal
Space Age Crystal

Reputation: 404

How to calculate numbers using international numeral systems in JavaScript/jQuery and HTML?

If you are calculating two numbers that use the notation of another numeral system, how do you convert the numbers so they can be used in calculations?

Upvotes: 1

Views: 408

Answers (1)

Space Age Crystal
Space Age Crystal

Reputation: 404

Using Globalize.js to Allow International Number Formatting

jQuery offers a script developed with Microsoft called globalize.js that formats numbers based on a locale. In this jsFiddle, I used Spain as the locale as an example, but its capable of many more formats and locales. Try using notation like 1.234,56 to check calculations. You can use the toLocaleString() function to format the calculated number back to a localized format, but this currently only works in Chrome and IE. You could use regex for the other browsers, but this is all I had time for. Here is the basic script, but look at the jsFiddle to see the other js required and the changes that needed to be made to the form tags.

<script>
    function Sumar() {
        var num1 = Globalize.parseFloat(document.getElementById("txtNumber1").value);
        var num2 = Globalize.parseFloat(document.getElementById("txtNumber2").value);
        resultado = num1 + num2;
        document.getElementById("p").innerHTML = "The result is " + resultado.toLocaleString("es-ES");
    }
</script>

Upvotes: 1

Related Questions