Kévin Blot
Kévin Blot

Reputation: 41

<audio> currentTime automatically change value

The currentTime attribute automatically change to 16.559999 when i set his value to 16.56 in javascript. I need the correct value which is 16.56.

Any ideas for what he change the value ?

Upvotes: 0

Views: 164

Answers (1)

daphtdazz
daphtdazz

Reputation: 8159

This is because javascript uses floating point numbers to represent all numbers, see w3schools. Floating point numbers cannot represent all numbers, because they only have a certain amount of accuracy. 16.56 is one of the (many) numbers that cannot be represented fully. This is similar to the way that we can't represent 1/3 accurately in decimal, because it needs a recurring decimal. See this discussion for more detail.

Two options to get round this are round to two decimal places or possibly do all operations multiplied by 100 then divide by 100 when you display the result.

Upvotes: 1

Related Questions