admin管理员组文章数量:1287150
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);
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);
Share
Improve this question
asked Feb 1, 2013 at 22:19
wowzuzzwowzuzz
1,38811 gold badges31 silver badges51 bronze badges
3
- You have strings, not numbers. – bfavaretto Commented Feb 1, 2013 at 22:21
-
2
You can cast strings to numbers using a
+
, though, sodt_total_hours += +'1'
will add1
, for instance. – Paul S. Commented Feb 1, 2013 at 22:40 - Thanks Paul and Maerics..it was a bination of the Number method with the single quotes. Very helpful! – wowzuzz Commented Feb 1, 2013 at 22:45
3 Answers
Reset to default 9If the values are strings then they will be concatenated, not added numerically.
Try constructing a number from the string value:
dt_total_hours += Number(res3.fieldByName(dt_cost_per_hour));
// ^------ Force a number here instead of a string.
Yeah, it's reading them as strings. I find that a dirty, awful hack to get them interpreted as numbers is to take x += y
and change it to x += y / 1.0
(or / 1
for integer). Usually does the trick.
you can parse your data according to desired type as follow
dt_total_hours += parseInt(res3.fieldByName(dt_cost_per_hour));
you can also use parseFloat method as well
本文标签: titaniumJavascript concatenating numbersnot adding upStack Overflow
版权声明:本文标题:titanium - Javascript concatenating numbers, not adding up - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741307635a2371465.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论