Accept comma in jquery / Javascript calculation

Asked

Viewed 821 times

1

The system does not work when typing number with comma, for example 2,14, 22,14 and so on... only with end point 2.14, 22.14 and so on... Below the function I use. How to use function with the number typed with comma, do not display ERROR Nan and the result also appear with comma. Would it be possible to restrict the use of dot in the field or even issue an alert if the user type "."? Thank you very much.

<script>
$(function(){
    $('.temp').change(function(){
        var total = 0;
        var notas = 0;
        $('.temp').each(function(){
            var nota = Number(this.value);
            if(nota === 0) return;
            total = total + nota;
            notas++;
        })
        var t = (total/notas).toFixed(2);
        $('#media_temperaturaE').val(t);
    });
})

Below the number typing screen to perform the calculation, with the two examples.

inserir a descrição da imagem aqui

  • Friend, take a look at this post: http://answall.com/questions/54691/mudar-ponto-por-virgula-em-value-de-input The solution is to replace the code, to change the comma to a point.

  • This . replace('.', ','); resolves the final result of the operation, but I still have to put "." for the calculation of my average function to work. I want a solution in which my function works with comma numbers, for example 2.1 and the result does not occur NAN ERROR.

  • The point is, I don’t know if Javascript accepts comma calculus. Programming languages usually divide the entire fraction using dots.

  • 1

    The Nan error happens because the value contained in the variable is a string "Not a Number".

  • http://www.w3schools.com/js/js_numbers.asp

1 answer

2


An alternative to your problem is to turn ',' into '.' while the user type:

            $('input[type="text"]').keyup(function(){
                var val = $($(this)).val().replace(',','.');
                $($(this)).val(val);
            });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="text" />

UPDATE See the above logic for a simple sum.

            $('input[type="text"]').keyup(function(){
                var val = $($(this)).val().replace(',','.');
                $($(this)).val(val);
                $('#result').val(Number($('input[name="number_01"]').val()) + Number($('input[name="number_02"]').val()));
            });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<form action="">
          <label for="">Number 01: </label><br>
          <input type="text" name="number_01" id="number_01"><br>
          <label for="">Number 02: </label><br>
          <input type="text" name="number_02" id="number_02"><br><br>
          <label for="">Result: </label><br>
          <input type="text" name="result" id="result">
        </form>

I hope I’ve helped.

  • Very nice solution also, however, the function does not work... when converts to comma already appears Nan in the total field... <script>&#xA; $(function(){&#xA; $('.temp').change(function(){&#xA; var total = 0;&#xA; var notas = 0;&#xA; $('.temp').each(function(){&#xA; var nota = Number(this.value);&#xA; if(nota === 0) return;&#xA; total = total + nota;&#xA; notas++;&#xA; })&#xA; var t = (total/notas).toFixed(2);&#xA; $('#media_temperaturaE').val(t);&#xA; });&#xA; })

  • Can even post HTML ?

  • 1

    All languages have the American default for integers and decimals, that is, you will not be able to do the calculation if you do not exchange the comma for the dot.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.