最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

why is 10 * (710) = 7 in JavaScript? - Stack Overflow

programmeradmin10浏览0评论

7 and 10 in the expression (7/10) are integers, so the result 0.7 should be integer as well, which is 0, and the result for the entire expression should be 0 too. However, it's giving me the result of 7, why? Is it ignoring the parentheses or converts to double automatically?

7 and 10 in the expression (7/10) are integers, so the result 0.7 should be integer as well, which is 0, and the result for the entire expression should be 0 too. However, it's giving me the result of 7, why? Is it ignoring the parentheses or converts to double automatically?

Share Improve this question asked Oct 11, 2013 at 11:29 Max KoretskyiMax Koretskyi 106k67 gold badges353 silver badges515 bronze badges 1
  • 2 Results don't depend on the format of the inputs, 10/4 is 2.5 - making that 3 would be wrong, but also annoying because it would mean having to write all numbers as their possible decimal accuracy when part of a sum, i.e. 10.0/4.0 and wouldn't be workable for 10/3 --- that result is pletely expected – MDEV Commented Oct 11, 2013 at 11:32
Add a ment  | 

7 Answers 7

Reset to default 11

JavaScript doesn't distinguish between integers and floating point numbers, everything I believe is considered a double so that is just why you get the result.

Take a look at the details on the Number property on MDN.

JavaScript doesn't have an integer type, or a double, or a float... it just has 1 type for all numbers: the helpfuly called Number type (try var foo = new Number(7);, or var foo = Number('123string');
Now, I know I said that JS doesn't know of floats, but that's not entirely true. All Number type vars/values are, essentially 64 bit floats, as defined by the IEEE 754 standard (which are, indeed, as Jan Dvorak kindly pointed out to me, double's in most staticly typed languages), with all the caveats that brings with it:

(.1 + .2);//0.30000000000000004

But that's not the point. The point is that, in JS you can perform float + int arithmatic without there ever being a need for internal casts, or conversions. That's why 10*(7/10) will always be 7

There is no int and double in JavaScript

In JavaScript, both int, flot, and double are normalized to work together. They are treated as 1 (They're treated as as Number, which is an IEEE 754 float. Thanks @Elias Van Ootegem). Equality, Liberty and Fraternity. and thus;

10*0.7 = 7

JavaScript is not like C.

Javascript doesn't have integers, and even if it did, there's nothing that says that / needs to return an integer (just because another language may do that doesn't mean every language has to). The operation results in a float/Number, just like all Javascript numbers are, period.

try this

10*parseInt(7/10)

hope this will help you

If you try to follow the rules, then

10 * (7/10) --> 10 * .7 --> 7

You cannot change the way its gonna result into.

so the result 0.7 should be integer as well, which is 0

If you want this, then try using

Math.Floor();

This would change the decimals to the nearest int! Or try out parse()

JavaScript uses dynamic types. That means that a variable like this:

var str = "hi";

Can later bee:

str = 123; //now we have an 'int'
str += 0.35; //now str is 123.35, a 'float'

So JavaScript doesn't cast floats to ints for example.

If you want to force a "cast" then you have to do:

var integer = parseInt( 3.14*9.0291+23, 10 ); //the second parameter (10) is the 'base'

But remember, Javascript will not take care of types, that's your problem.

发布评论

评论列表(0)

  1. 暂无评论