简体   繁体   中英

Parsing a float number in JavaScript using different browsers

I got a web page with some input controls, pe 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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM