如何使用 javascript w/o toFixed() 在 bbc:microbit 上四舍五入到 1 位小数,通常的乘法 + 除法给出尾随 9999 位数字?

How do I round to 1 decimal on the bbc:microbit with javascript w/o toFixed(), the usual multiplication + divide gives trailing 9999 digits?

temperatureReading = Math.round(temperatureReading * 10) / 10

给我 26.29999999999999999999 而不是 26.3

和 26.00000000001 而不是 26.0

我从温度传感器得到两个交替值:26.33 和 26.3200000

转换后我有:26.2999999999999

上面的重复数字只是一个例子。我在micro bit上的显示不够宽,看不到它们。

我使用 toString() 来显示值。

遗憾的是,toFixed() 和 toPrecision() 在 micro:bit

上不可用

四舍五入可以通过其他一些操作实现吗?

使用以下代码,我现在可以将带小数点后一位的数字作为字符串:

let temperatureStr = Math.round(temperatureReading * 10).toString()
let temperature = temperatureStr.slice(0, temperatureStr.length - 1) + "." + temperatureStr.slice(temperatureStr.length - 1);

我先将数字乘以 10,然后将结果转换为字符串。然后,我在最后一位数字前插入小数点。这给了我要打印的字符串。