Timer (ms) Displayed as String

This is a simple 2 line custom function I wrote to convert the actual milliseconds from the timer into a string for display. In my specific use case, I'm tracking user movements so didn't want to add another read each time there is an update for a string instead. With this function, I can use the milliseconds for calculations but then display the milliseconds in a minute/second string in my application!


import 'dart:math' as math;

String mstoString(double timeinms) {
  // Convert milliseconds to minutes and seconds and then convert that into a string
  String newString = (timeinms / 100000).toStringAsFixed(2);
  return newString.replaceAll('.', ':');
}
Example:ย 2000 ms (integer) -> 0:02 (string)
[image.png][image.png]
2
1 reply