2

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.

Jordash
  • 2,926
  • 8
  • 38
  • 77
  • See here http://stackoverflow.com/questions/2686855/is-there-a-javascript-function-that-can-pad-a-string-to-get-to-a-determined-lengt. Also check [toFixed](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed) – elclanrs Aug 15 '16 at 03:44
  • 1
    Are the leading zeros that you're adding supposed to make it into a four-digit number, or do you want to add them even if the number already has four or more digits to the left of the decimal place? In other words, what should the output be for input of `100.11` and `1000.22`? – nnnnnn Aug 15 '16 at 03:46

1 Answers1

1

This should work:

trackPartLength = ("0000" + trackPartLength.toFixed(2)).slice(-7).replace( /\.00$/, '' );

It uses toFixed to get the two decimal points, zero pads and then removes any trailing .00. Examples:

48.4532     -> 0048.45
48          -> 0048
6.213       -> 0006.21
12345       -> 2345
1234.56789  -> 1234.57

If the number can have more than four digits before the decimal, as in the 12345 example, you may want to make the zero padding and slice conditional, so that you don't remove leading digits from a big number. That could be done like this:

var tmp = trackPartLength.toFixed(2);
trackPartLength = (tmp.length >= 7 ? tmp : ('0000' + tmp).slice(-7)).replace( /\.00$/, '' );
Paul
  • 139,544
  • 27
  • 275
  • 264
  • Should probably use `/(\.00)|0$/` (or similar) to allow for `0001.20` – nnnnnn Aug 15 '16 at 03:53
  • @nnnnnn It's not clear to me if the OP wants the output to be `0001.2` or `0001.20` for that case. "I also don't want to show decimals unless they are necessary" is somewhat ambiguous (since it's not clear if they mean all the decimals or individual decimals), and they only examples they gave show two `0`. – Paul Aug 15 '16 at 04:03
  • @nnnnnn The suggestion is good though, but that particular regex could have issues with inputs like `50`, since it would strip the `0` there too. Something like `.replace( /\.00/, '' ).replace( /(\.\d)0/, '$1' )` would be better. – Paul Aug 15 '16 at 04:05
  • My suggested regex won't strip the `0` from `50` *in this context*, because the string you are using it on is the result of `.toFixed(2)` and so is guaranteed to have two decimal places. – nnnnnn Aug 15 '16 at 04:13
  • Sorry for not being clear, I don't want to show any trailing zeroes, so instead of `0001.20` it should be `0001.2`, this is awesome! – Jordash Aug 15 '16 at 04:23