jsmreese / moment-duration-format

Format function plugin for the Moment Duration object.
MIT License
968 stars 120 forks source link

Allow specifying number of significant figures, instead of "precision" #74

Closed gabrielgrant closed 6 years ago

gabrielgrant commented 7 years ago

The precision option allows controlling number of figures after the decimal place, but it often makes more sense to control the total number of significant figures. So, for example, setting the number of significant figures to 3 with template 's[s]' would output 10.3s, 1.32s, or 0.323s

There's a bit of complication with multiple units, but it seems like the same concept should extend. So 'h[h] m[m] s[s]' would become 12h 30m, 1h 23m, 12m 30s, 1m 23s, 12.3s, 1.23s, 0.123s or 0.0123s

Would also be nice to have a way of specifying that any digits required for padding to the left of the decimal point be populated. So, even if 3 sig figs were requested, instead of getting 12h 30m you'd get something like 12h 34m, but if no padding is needed, you'd still have 1h 23m

jsmreese commented 6 years ago

Interesting idea. I'll try to take a look at this next week.

gabrielgrant commented 6 years ago

Awesome, thanks!

jsmreese commented 6 years ago

Pushed a new version just now. Setting useSignificantDigits to true will treat precision as the maximum significant digits to display. Rounding is the default behaviour now, but it's compatible with using trunc as well.