简体   繁体   English

JavaScript错误中的整数除法

[英]Integer division in JavaScript Error

When i try to divide and get a result using the following equation in Javascript 当我尝试使用Javascript的以下方程式进行除法并获得结果时

var level = (userLevel + 1)/2;

where userLevel = 7 其中userLevel = 7

i get 35.5 as a result instead of 4 ? 我得到的结果是35.5 ,而不是4?

what am i doing wrong ? 我究竟做错了什么 ?

userLevel is a string, use userLevel是一个字符串,使用

var level = (parseInt(userLevel) + 1)/2;

7+1 evaluates to 71 as 7 is a string 7 + 1计算为71,因为7是字符串

javascript will consider your userLevel as a sttring for that you have convert the type into int or parse it to int javascript会将您的userLevel视为字符串,因为您已将类型转换为int或将其解析为int

Try this : 尝试这个 :

var level = ((userLevel-0) + 1)/2;

Cheers :-) 欢呼声:-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM