Incorrect Javascript math result, removing NaN message - Stack Overflow

I'm trying to run the following sum in JavaScript (a + b) - cbut unfortunately I keep getting the

I'm trying to run the following sum in JavaScript

(a + b) - c

but unfortunately I keep getting the following result (example sun):

(25 + 25) - 1

= 2524

here is the code im using at the moment

     <script>
    $(document).ready(function(){
      $('#previousRent').change(function(){
        calcResult();
      });
      $('#rentPaid').change(function(){
        calcResult();
      });
      $('#wRun').change(function(){
        calcResult();
      });
    });
    function calcResult() {
      $('#result').val( parseFloat($('#previousRent').val() + parseFloat($('#wRun').val()) - $('#rentPaid').val()) );
    }
  </script>   

Does anyone know how to get rid of the NaN that appears when all fields are not filled?

I'm trying to run the following sum in JavaScript

(a + b) - c

but unfortunately I keep getting the following result (example sun):

(25 + 25) - 1

= 2524

here is the code im using at the moment

     <script>
    $(document).ready(function(){
      $('#previousRent').change(function(){
        calcResult();
      });
      $('#rentPaid').change(function(){
        calcResult();
      });
      $('#wRun').change(function(){
        calcResult();
      });
    });
    function calcResult() {
      $('#result').val( parseFloat($('#previousRent').val() + parseFloat($('#wRun').val()) - $('#rentPaid').val()) );
    }
  </script>   

Does anyone know how to get rid of the NaN that appears when all fields are not filled?

Share Improve this question edited Jun 4, 2016 at 15:32 halfer 20.4k19 gold badges109 silver badges202 bronze badges asked Nov 24, 2010 at 9:06 methuselahmethuselah 13.2k53 gold badges177 silver badges333 bronze badges
Add a ment  | 

6 Answers 6

Reset to default 8

I think you should re-arrange the parentheses a bit:

function calcResult() {  
  $('#result').val( (parseFloat($('#previousRent').val()) + parseFloat($('#wRun').val())) - $('#rentPaid').val() );  
} 

Edit: Some explanation: Note that the val() function returns the value of the element, which is a string. In javascript the "+"-operator is used for concaterating strings which means that you are sending in "2525" to the first parseFloat-call..

You have a problem with brackets. This is the right line:

$('#result').val( parseFloat($('#previousRent').val()) + parseFloat($('#wRun').val()) - parseFloat($('#rentPaid').val()) );

changes function calcResult() with below content

function calcResult() {
    $('#result').val(parseFloat($('#previousRent').val()) + parseFloat($('#wRun').val()) - parseFloat($('#rentPaid').val()));
}

You are trying to add strings, which concatenates them. I don't know jQuery but you need to make sure your variables end up as integer or float values so it can evaluate the match, not the strings.

I would do something like this, in case the fields aren't populated:

function calcResult() {  
 $('#result').val(
  (parseFloat($('#previousRent').val()||"0") + parseFloat($('#wRun').val()||"0"))
    - parseFloat($('#rentPaid').val()||"0"));  
} 

This would treat any empty field as 0 instead of throwing a NaN calculation error. Or, a bit cleaner version:

function getVal(sel) { return parseFloat($(sel).val()||"0"); }
function calcResult() {  
  $('#result').val(getVal('#previousRent') + getVal('#wRun') - getVal('#rentPaid'));  
} 

No need to use the brackets when you are just using (24+24)-1

Just use like this 24+24-1

pasrse each string to float separatly

You have used

    $('#result').val( 
      parseFloat($('#previousRent').val() 
    + parseFloat($('#wRun').val()) 
    - $('#rentPaid').val()) );

Use this

        $('#result').val( 
    parseFloat($('#previousRent').val()) 
    + parseFloat($('#wRun').val()) 
    - parseFloat($('#rentPaid').val()) );

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743777176a4505446.html

相关推荐

  • Incorrect Javascript math result, removing NaN message - Stack Overflow

    I'm trying to run the following sum in JavaScript (a + b) - cbut unfortunately I keep getting the

    7天前
    90

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信