Hi I'm working on a function which involves multiplying by 0.5 and I've got a bit of a problem with it. I think I'll be able to solve it in overall, however, I just wonder if multiplying by 0.5 in javascript could be causing a trouble. Sorry I tried searching over the Internet but with no success.
Ps. I don't post the function, I don't want to give up yet;)

As far as I know there's no particular issue with `* 0.5` , however when performing any arithmetic calculation, javascript can give unexpected (imprecise) results due to all numbers being floating point. There's no such thing as a genuine, guaranteed integer value as exists in most programming languages.

The …

## All 2 Replies

As far as I know there's no particular issue with `* 0.5` , however when performing any arithmetic calculation, javascript can give unexpected (imprecise) results due to all numbers being floating point. There's no such thing as a genuine, guaranteed integer value as exists in most programming languages.

The good news is that parseint(), Math.round(), Math.floor() and Math.ceil() all reliably return floating point representations of integer values, and such returned values print out as integers with eg. alert() or DOMelement.innerHTML=... ; you don't get a string of zeros to the right of the decimal point. `myEvenIntegerValue * 0.5` could conceivably return a non-integer result but only if myEvenIntegerValue was an imprecise "near integer" result of a previouis calculation.

I expect that's what you are experiencing. If so, then sanitize your value with Math.round();

Airshow

As far as I know there's no particular issue with `* 0.5` , however when performing any arithmetic calculation, javascript can give unexpected (imprecise) results due to all numbers being floating point. There's no such thing as a genuine, guaranteed integer value as exists in most programming languages.

parseint(), Math.round(), Math.floor() and Math.ceil() all return floating point representations of integer values. The good news is that such returned values print out as integers with eg. alert() or DOMelement.innerHTML=... . `myEvenIntegerValue * 0.5` could conceivably return a non-integer result but only if myEvenIntegerValue was an imprecise "near integer" result of a previouis calculation.

I expect that's what you are experiencing. If so, then sanitize your value with Math.round();

Airshow

Wow great thx I finally get it

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.