1

I got a web page with some input controls, p.e. for a subtraction.

    <table>
        <tr>
            <td><b>Value 1</b></td>
            <td />
            <td><b>Value 2</b></td>
            <td><b>Result</b></td>
        </tr>
        <tr>
            <td>
                <%--  step="0.01" because of Firefox-browser --%>
                <input type="number" id="value11" oninput="SubtractionWithParameters(document.getElementById('value11').value, document.getElementById('value21').value, 'result', 2)" step="0.01" />
            </td>
            <td> - </td>
            <td>
                <input type="number" id="value21" oninput="SubtractionWithParameters(document.getElementById('value11').value, document.getElementById('value21').value, 'result', 2)" step="0.01" />
            </td>
            <td>
                <input type="number" id="result" step="0.01" />
            </td>
        </tr>
    </table>

For the subtraction i defined the JavaScript function SubtractionWithParameters which calls the function Subtraction. While entering numbers, dots and/or commas in the input controls "value11" and "value21" the function SubtractionWithParameters is called and calculates the result at once.

        //Subtracts the given parameters and displays the result in the input-Control with the given controlname.
        function SubtractionWithParameters(value1, value2, resultControlName, decimalNumber) {
            decimalNumber = typeof decimalNumber == 'undefined' ? 0 : decimalNumber;
            document.getElementById(resultControlName).value = Subtraction(value1, value2).toFixed(decimalNumber);
        }

        //Subtracts the given parameters and returns the result.
        function Subtraction(value1, value2) {
            if (isNaN(value1) || value1 == "") { value1 = 0; }
            if (isNaN(value2) || value2 == "") { value2 = 0; }
            var result = parseFloat(value1) - parseFloat(value2);
            return result;
        }

Now the problem is that IE and Firefox expecting different inputs for a floatnumber. IE: 0.36 (with dot) Firefox: 0,36 (with comma)

When i use Firefox and input in one of my input controls a floatnumber which contains a dot, nothing happens. How can i handle this in my function Subtraction?

Thanks in advance!

Patrick Pirzer
  • 1,649
  • 3
  • 22
  • 49
  • 1
    If IE and FF are expecting different formats, they're probably set to different locales. – ssube Aug 15 '16 at 15:13
  • 1
    According to the answers to [this question](http://stackoverflow.com/questions/2085275/what-is-the-decimal-separator-symbol-in-javascript), and [this one](http://stackoverflow.com/questions/12694455/javascript-parsefloat-in-different-cultures), `parseFloat()` expects dots specifically, and doesn't do commas, regardless of the locale. But a comma after digits won't cause an error: `parseFloat("12,34")` should return `12`. – nnnnnn Aug 15 '16 at 15:16
  • Thanks a lot! Adding ".replace(',','.')" when giving the values to the function helps. – Patrick Pirzer Aug 16 '16 at 06:59

1 Answers1

0

Thanks a lot! Adding ".replace(',','.')" when giving the values to the function helps.

Patrick Pirzer
  • 1,649
  • 3
  • 22
  • 49