Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
265 views
in Technique[技术] by (71.8m points)

titanium - Javascript concatenating numbers, not adding up

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);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If 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.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...