EvanHahn / HumanizeDuration.js

361000 becomes "6 minutes, 1 second"
https://evanhahn.github.io/HumanizeDuration.js/
The Unlicense
1.65k stars 175 forks source link

Add an ability to round to custom number of decimal points #73

Closed mickeyvip closed 7 years ago

mickeyvip commented 9 years ago

Hi and thank you!

I think it would be a great addition to have be able to round to a specific number of decimal places:

now:

var smallDuration = 499;
var viewValue = humanizeDuration(smallDuration); // -> "0.499 seconds"
var viewValueRounded = humanizeDuration(smallDuration, {round: true}); // -> "0 seconds"

would be nice:

var smallDuration = 499;
var viewValueRounded = humanizeDuration(smallDuration, {round: 1}); // -> "0.5 seconds"
EvanHahn commented 9 years ago

Could this just proxy to toFixed?

(0.499).toFixed(1)  // => "0.5"
(0.499).toFixed(2)  // => "0.50"
(0.499).toFixed(3)  // => "0.499"
mickeyvip commented 9 years ago

If I proxy it to .toFixed() I would lose the time units.

EvanHahn commented 8 years ago

If you'd like to submit a PR, feel free!

marcj commented 8 years ago

That would be awesome :)

topr commented 7 years ago

Perhaps round param could take a function instead of boolean value. This would be far more flexible as I need for example to cut off seconds, not to round them.

EvanHahn commented 7 years ago

@topr Agreed. One thing that makes this tricky is how rounding affects other numbers. For example, what happens if you do something like this?

humanizeDuration(123456, {
  round: function () {
    return 0
  }
})
topr commented 7 years ago

Would it be an issue? Not sure but I guess result should then be the same as if 0 would be passed in instead of 123456, shouldn't it?

I don't think the lib should care or prevent such a misuse. If someone passes a custom impl then it's his responsibility to make it right.

I was mostly thinking about use cases like humanizeDuration(123456, { round: Math.floor })

On 2 Feb 2017 22:58, "Evan Hahn" notifications@github.com wrote:

@topr https://github.com/topr Agreed. One thing that makes this tricky is how rounding affects other numbers. For example, what happens if you do something like this?

humanizeDuration(123456, { round: function () { return 0 } })

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/EvanHahn/HumanizeDuration.js/issues/73#issuecomment-277111919, or mute the thread https://github.com/notifications/unsubscribe-auth/AA1TIpAvF0-3Xsd6UjHiXpK98VDnSJv2ks5rYl-igaJpZM4FckB4 .

EvanHahn commented 7 years ago

What if you're trying to humanize the largest 2 units and then you choose to round down? For example, your result might start like this before you round and do your cutoff:

2 weeks, 6 days, 23 hours, 59 minutes

Depending on how you round, the results can be different. If you round up, you could get "3 weeks". If you round down, you should get "2 weeks, 6 days". These rounding issues can cascade, which is why it's tricky (but not impossible).

vincentjames501 commented 7 years ago

+1. This would be nice.

stevenvachon commented 7 years ago

Do it.

EvanHahn commented 7 years ago

Lots of people seem to want this feature and things around it. I want to do it but I'm worried it gets really hairy with lost of corner cases. What do people think about my comment from February?

stevenvachon commented 7 years ago
{
  units: ['d','m','s'],
  unitsConfig: {
    m: {
      label: 'min',  // languages
      measure: 30,  // unitMeasures
      round: value => 1
    }
  }
}
EvanHahn commented 7 years ago

This library is in maintenance mode, so I won't be adding new features, sadly. I'm going to close this issue. See #120 for more.

stevenvachon commented 7 years ago

Use pretty-ms instead.