Microsoft MakeCode

Math.roundWithPrecision issue

From a message on Twitter:


I have the code: let lectura = 0
input.onButtonPressed(Button.A, function () {
lectura = pins.analogReadPin(AnalogPin.P0)
basic.showNumber(escalado(lectura))
})
function escalado(valor: number) {
let resultado = 0
resultado = (valor * 3) / 1023
return Math.roundWithPrecision(resultado, 2)

}

the idea is measure volt from input

I want only see for example 1.47 when I measure a small battery

that is rigth into emulator screen but when I load the program I see 1.4699999999…

why do I see this result into the led screen? could you help me to understand it?


I think this might be a bug with “roundWithPrecision”
@michal, do we know if Math.roundWithPrecision is expected to work on HW?

I believe it rounds - the number seems rounded to two decimal points. It just that we print it out wrong, the code is rather simplistic https://github.com/microsoft/pxt-common-packages/blob/master/libs/base/core.cpp#L1088

Printing out floating point numbers is rather a complex task.