[JS] on toFixed and banker rounding method

learn that the rounding rule of JavaScript"s toFixed method is banker rounding

.

that is:

rounded into consideration, five is not zero, five is zero, five is parity, five years ago is even should give up, five before is odd to enter one
11.556 = 11.56-six
11.554 = 11.55-four
11.5551 = 11.56-five after rounding
11.545 = 11.54-innumerable after five, If the leading position is even, you should leave out
11.555 = 11.56-innumerable after five. If the leading position is odd, you should carry
.

so in my understanding, the result of (0.45) .tofixed (1) should be 0.4, that is, after five innumerable, if the leading position is even, we should leave out

.

ran goose

clipboard.png

(0.075) .tofixed (2)
"0.07"
(0.085) .tofixed (2)
" 0.09"
(0.045) .tofixed (2)
"0.04"

does anyone know? Ask for advice

(er, there may be some holes in this question-_-| |)

Mar.16,2021

. I don't know where it looks, but if so, it is suggested to indicate the source to prevent others from entering the pit again.

I think MDN's toFixed entry page translates quite well here:

The
toFixed () method uses fixed-point notation to format a number.

because this "Fixed" means "fixed-point", so, this is a problem of loss of accuracy when binary floating point numbers are converted to fixed point numbers. The carry you see is actually an inevitable loss of floating point numbers in binary-decimal conversion. There is a online number system conversion on the Internet. You can try it (of course, I recommend that you learn about integers, fixed-point numbers, and floating-point numbers, and the conversion between them, which will be understood by hand).

< hr >

add an article for reference:

Why (2.55) .tofixed (1) equals 2.5?


after Pu Liu's seclusion , it is suspected that it is a floating point number loss problem

.

has done the following experiments:

(0.45).toFixed(20)
"0.45000000000000001110"
Menu