te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>jquery - Cannot add two numbers correctly without using parseFloat in JavaScript - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Cannot add two numbers correctly without using parseFloat in JavaScript - Stack Overflow

programmeradmin4浏览0评论

I came across this problem. It adding numbers using parseFloat or parseInt. IF textbox1 value is 4 and textbox2 value is 2 then i got output as (see script)

My doubt is why in addition alone

parseFloat($('#txt1').val()) + parseFloat($('#txt2').val())

gives correct value but

parseFloat($('#txt1').val() + $('#txt2').val())

is not giving correct value whereas

  • parseFloat($('#txt1').val() - $('#txt2').val()),
  • parseFloat($('#txt1').val() / $('#txt2').val()),
  • parseFloat($('#txt1').val() * $('#txt2').val())

are giving correct value. Its simple but i couldn't find solution.

=====jQuery

   function Calculate() {                                              //--> Output
     $('#lbl1').html(parseFloat($('#txt1').val() + $('#txt2').val())); //--> 42
     $('#lbl2').html(parseFloat($('#txt1').val()) + parseFloat($('#txt2').val())); //--> 6
     $('#lbl3').html(parseFloat(4 + 2));                               //--> 6

     $('#lbl4').html(parseFloat($('#txt1').val() - $('#txt2').val())); //--> 2
     $('#lbl5').html(parseFloat($('#txt1').val() * $('#txt2').val())); //--> 8
     $('#lbl6').html(parseFloat($('#txt1').val() / $('#txt2').val())); //--> 2
  }

=====HTML

<table>
        <tr>
            <td>
                <input type="text" id="txt1" />
            </td>
            <td>
                <input type="text" id="txt2" />
            </td>
        </tr>
        <tr>
            <td>
                <input type="button" value="Calculate"  onclick="Calculate()" />
            </td>
            <td>
                <label id="lbl1">
                </label>
                |
                <label id="lbl2">
                </label>
                |
                <label id="lbl3">
                </label>
                |
                <label id="lbl4">
                </label>
                |
                <label id="lbl5">
                </label>
                |
                <label id="lbl6">
                </label>
            </td>
        </tr>
    </table>

I came across this problem. It adding numbers using parseFloat or parseInt. IF textbox1 value is 4 and textbox2 value is 2 then i got output as (see script)

My doubt is why in addition alone

parseFloat($('#txt1').val()) + parseFloat($('#txt2').val())

gives correct value but

parseFloat($('#txt1').val() + $('#txt2').val())

is not giving correct value whereas

  • parseFloat($('#txt1').val() - $('#txt2').val()),
  • parseFloat($('#txt1').val() / $('#txt2').val()),
  • parseFloat($('#txt1').val() * $('#txt2').val())

are giving correct value. Its simple but i couldn't find solution.

=====jQuery

   function Calculate() {                                              //--> Output
     $('#lbl1').html(parseFloat($('#txt1').val() + $('#txt2').val())); //--> 42
     $('#lbl2').html(parseFloat($('#txt1').val()) + parseFloat($('#txt2').val())); //--> 6
     $('#lbl3').html(parseFloat(4 + 2));                               //--> 6

     $('#lbl4').html(parseFloat($('#txt1').val() - $('#txt2').val())); //--> 2
     $('#lbl5').html(parseFloat($('#txt1').val() * $('#txt2').val())); //--> 8
     $('#lbl6').html(parseFloat($('#txt1').val() / $('#txt2').val())); //--> 2
  }

=====HTML

<table>
        <tr>
            <td>
                <input type="text" id="txt1" />
            </td>
            <td>
                <input type="text" id="txt2" />
            </td>
        </tr>
        <tr>
            <td>
                <input type="button" value="Calculate"  onclick="Calculate()" />
            </td>
            <td>
                <label id="lbl1">
                </label>
                |
                <label id="lbl2">
                </label>
                |
                <label id="lbl3">
                </label>
                |
                <label id="lbl4">
                </label>
                |
                <label id="lbl5">
                </label>
                |
                <label id="lbl6">
                </label>
            </td>
        </tr>
    </table>
Share Improve this question edited Jun 21, 2015 at 8:58 royhowie 11.2k14 gold badges53 silver badges67 bronze badges asked Apr 20, 2013 at 7:43 Lakshmana KumarLakshmana Kumar 1,2493 gold badges17 silver badges34 bronze badges 2
  • 3 This is because the + operator is also used in strings, while the - and * operators are not. With loose-typed languages like this you get this behaviour. – Kaiwa Commented Apr 20, 2013 at 7:46
  • 1 I'm surprised no one suggested the excellent jQuery arithmetic plugin. – Benjamin Gruenbaum Commented Jun 21, 2015 at 8:49
Add a ment  | 

2 Answers 2

Reset to default 10

$.val() returns a string value.

So in your first example you convert both returned strings to numbers and the calculation is fine.

If you use parseFloat($('#txt1').val() + $('#txt2').val()) the + does not work as the arithmetic operator, but as a string concatenation. So you concatenate both strings and convert them afterwards, which gives a wrong result.

The examples using - will work, as there is no string operation using - and by thus alls values get implicitly converted to a number before the operation is applied.

$('#txt1').val() + $('#txt2').val() it gives String value

you can not use - , * , /operator on strings

parseFloat($('#txt1').val()), parseFloat($('#txt2').val()) returns numbers not strings

发布评论

评论列表(0)

  1. 暂无评论