I hope to keep a number to two decimal places. If there are decimal places, round them off. If not, fill in 0.
For example,
1=》1.00
1.234=》1.23
1.256=》1.26
I tried using the toFixed function, and its content is correct, but it returns a string. For example, 1.00 is a string. When I explicitly convert Number(1.00), it becomes 1.
So how do I get the number to retain two decimal places and the return type is a number?
My guess is: If it is used for page display, then strings don’t matter, right? If it is used for operation, it doesn't matter even if it is 1 (not 1.00), right?
1.00 This format can only be saved as a string, and cannot be saved as a numeric type.
No matter what you do, if you want to display something like 1.00, you can only use strings.
First of all, the toFixed method itself is buggy. Secondly, the Number structure in js does not contain precision. If you need numbers that contain precision, you can just write a class yourself
parseFloat(1.236.toFixed(2))