How can an unknown number be converted into a string that still retains the value after the decimal point?

an unknown number, and I don"t know whether it has a decimal point or how many digits it takes

to convert it to a string

the decimal point cannot be omitted

there may be many zeros after the decimal point

for example: 18, 20.3, 3.6, 3.001, 3.0000

what is commonly used is str = num +""; but unfortunately 3.0000 +" = 3 .

Is there any way to solve this problem?

Nov.09,2021

when tried in the Chrome console, the last zeros after the decimal point are automatically discarded. So it is recommended that after you get the number, you still need to get a few decimal places after this number. Then call the toFixed method.
clipboard.png


if you ask this question, it means you don't understand how the numbers are stored in js. In fact, not only most js, languages are the same, does not care about, does not distinguish, and will not store a small number with several zeros at the end of the decimal part. 3.0,3.00 are exactly the same in memory according to the IEEE754 specification. If you have to make a distinction, you can only use string storage or use your own designed data structures. But generally there will not be such a requirement as you mentioned, you can put your usage scenario up, there should be another solution.

< hr >

if you just keep a fixed number of decimal places, use the words toFixed upstairs

.

see, what I get is originally a string, so there is a situation where the decimal point is all zero, and there is no need for conversion at all. Thank you both upstairs for your answers

Menu