I have a number of variable length the looks something like this:
48.4532
I want to convert it to 4 digits before decimal and 2 decimal places, so the number above should be:
0048.45
I also don't want to show decimals unless they are necessary, so:
48
should become:
0048
I was able to get the fixed length, but I couldn't get the decimals to show up only if they were necessary (I don't want to show two 0's at the end).
This is how I got a fixed length:
trackPartLength = ("0000" + trackPartLength.toString()).slice(-4); // Convert to fixed length
How do I add the 2 decimal points only if they are needed?
Edit: I also just realized that if the number does have decimals with the above code, it moves the decimal point over 4 spots causing some other problems, so I'm not sure if my initial approach is a good one. I'm trying to right a fixed length function of variable fixed prefix and decimal length.