开发者

Javascript Adding Values Together

I'm trying to add two values together e.g. 14.0 + 2.1 = 16.1 but I keep o开发者_如何学运维n getting them added onto each other e.g 14.0 + 2.1 = 14.02.1

var miledistance = miledistance1 + miledistance2;


For Java:

Make sure they are both float values.
Try casting:

miledistance = (float) miledistance1 + (float) miledistance2;

Or use Float.valueOf():

miledistance = Float.valueOf(miledistance1) + Float.valueOf(miledistance2);

For Javascript:

miledistance = parseFloat(miledistance1) + parseFloat(miledistance2);

NOTE: Java and javascript are not the same language.


It seems to be that the program is treating them as strings, cast them as float or double.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜