<audio> currentTime 自动更改值
<audio> currentTime automatically change value
当我在 javascript 中将他的值设置为 16.56 时,currentTime 属性自动更改为 16.559999。
我需要正确的值 16.56。
他改变值有什么想法吗?
这是因为javascript使用浮点数来表示所有的数字,详见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。
解决这个问题的两个选项是舍入到小数点后两位,或者可能将所有运算乘以 100,然后在显示结果时除以 100。
当我在 javascript 中将他的值设置为 16.56 时,currentTime 属性自动更改为 16.559999。 我需要正确的值 16.56。
他改变值有什么想法吗?
这是因为javascript使用浮点数来表示所有的数字,详见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。
解决这个问题的两个选项是舍入到小数点后两位,或者可能将所有运算乘以 100,然后在显示结果时除以 100。