0

I've got a function that takes in milliseconds and returns a formatted string of larger units of time. For example, if you pass in 74600 milliseconds, it returns the string "1:14.600".

My issue right now is I want it to only return the first two digits of the milliseconds place, but I'm not exactly sure how to do that. I was thinking about truncating the string, but I think you can only do that based on length, and if there were two digits in the minutes place it would throw that off.

Here's my timeFormatter() function:

function timeFormatter(s) {
        var ms = s % 1000;
        s = (s - ms) / 1000;
        var secs = s % 60;
        s = (s - secs) / 60;
        var mins = s % 60;
        var hrs = (s - mins) / 60;

        if (hrs > 0) {

        } else {
            if (mins > 0) {
                if (secs < 9) {
                    if (ms < 99) {
                        return mins + ':0' + secs + '.0' + ms;
                    } else {
                        return mins + ':0' + secs + '.' + ms;
                    }
                } else {
                    if (ms < 99) {
                        return mins + ':' + secs + '.0' + ms;
                    } else {
                        return mins + ':' + secs + '.' + ms;
                    }
                }
            } else {
                if (ms < 99) {
                    return secs + '.0' + ms;
                } else {
                    return secs + '.' + ms;
                }
            }
        }
    }

I know there's that big gross if statement in there, I'm not really concerned with simplifying that yet.

Thanks!

EDIT: I should add that with smaller strings (under a minute) to fix this problem, I just used toFixed(2) to keep it at only two decimal places, so this problem only exists with larger strings (over a minute).

Treedot
  • 29
  • 5
  • You could use the [subString()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring) method on the ms variable so it only returns the first two characters. It would look something like this: `ms.subString(0, 1)` – hajile78 Jul 18 '18 at 15:08
  • My only point of confusion with that is that the pieces aren't a string until they're returned, so I don't think I can use subString on just the ms variable. If I try, it doesn't work. – Treedot Jul 18 '18 at 15:11
  • As a note, you can replace your zero-padding mega-if with something like [this](https://stackoverflow.com/a/9744576/836214) – Krease Jul 18 '18 at 15:12
  • @hajile78 i think, this is the case, but to get first two character of a string we have to write `ms.substring(0,2)`, but it will produce output as '1.' in case of `'1.343'.substring(0,2)` – Abhishek Kumar Jul 18 '18 at 15:12
  • @AbhishekKumar Thanks for catching that! – hajile78 Jul 18 '18 at 15:15

1 Answers1

1

Id recommend just performing math on your ms variable, before you concatenate it with the other variables.

ms = Math.floor(ms/10);//if you want to simulate integer division (truncation)

ms = Math.round(ms/10);//if you want to round the last digit of precision
dgeare
  • 2,618
  • 5
  • 18
  • 28