Javascript连接数字,而不是加起来 [英] Javascript concatenating numbers, not adding up

查看:496
本文介绍了Javascript连接数字,而不是加起来的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它一直将我的数字连接到2111而不是5.这是为什么?我尝试过使用parseInt但没有运气。 res3 btw表示我正在执行的数据库查询。

It keeps concatenating my numbers into 2111 instead of 5. Why is this? I've tried using parseInt with no luck. res3 btw represents a query into my database that I'm executing.

var dt_total_hours = 0;

            dt_total_hours += res3.fieldByName(dt_cost_per_hour);
            dt_total_hours += res3.fieldByName(dt_prod_dt_hours);
            dt_total_hours += res3.fieldByName(dt_prod_rate);
            dt_total_hours += res3.fieldByName(dt_cost_per_unit);
            dt_total_hours += res3.fieldByName(dt_scrap_startup_cost);
            dt_total_hours += res3.fieldByName(dt_labor_expense);
            dt_total_hours += res3.fieldByName(dt_since_issues_first_noticed);
            dt_total_hours += res3.fieldByName(dt_wo_for_maint);
            dt_total_hours += res3.fieldByName(dt_investigation);
            dt_total_hours += res3.fieldByName(dt_maint_made_bandaid);
            dt_total_hours += res3.fieldByName(dt_parts_outsourcing);
            dt_total_hours += res3.fieldByName(dt_get_equip_out_prod);
            dt_total_hours += res3.fieldByName(dt_perm_repair);
            dt_total_hours += res3.fieldByName(dt_equip_back_to_prod);
            dt_total_hours += res3.fieldByName(dt_to_full_prod_speed);
            dt_total_hours += res3.fieldByName(dt_other);


推荐答案

如果值是字符串,那么它们将被连接,没有以数字方式添加。

If the values are strings then they will be concatenated, not added numerically.

尝试从字符串值构造一个数字

dt_total_hours += Number(res3.fieldByName(dt_cost_per_hour));
//                ^------ Force a number here instead of a string.

这篇关于Javascript连接数字,而不是加起来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆