EvanHahn / HumanizeDuration.js

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

User can't control a precision for floats #139

Closed jvorcak closed 5 years ago

jvorcak commented 6 years ago

If I create a custom language and pass a number 8255.28, it outputs 8s 255.28000000000065ms which is not wanted.

It happens because of this line https://github.com/EvanHahn/HumanizeDuration.js/blob/master/humanize-duration.js#L493

const humanizer = humanizeDuration.humanizer({
    language: 'shortEn',
    spacer: '',
    delimiter: ' ',
    decimal: '.',
    languages: {
      shortEn: {
        y: function () {
          return 'y'
        },
        mo: function () {
          return 'mo'
        },
        w: function () {
          return 'w'
        },
        d: function () {
          return 'd'
        },
        h: function () {
          return 'h'
        },
        m: function () {
          return 'm'
        },
        s: function () {
          return 's'
        },
        ms: function () {
          return 'ms'
        },
      },
    },
    units: ['y', 'mo', 'w', 'd', 'h', 'm', 's', 'ms'],
  })
EvanHahn commented 6 years ago

To quote the readme, HumanizeDuration.js "is actively maintained but no new features will be added." This sounds like a new feature, unfortunately.

I realize it's not as powerful as the maxDecimalPoints feature you added in #140, but would the round option solve this problem?

jvorcak commented 6 years ago

Not really. Using round is not powerful enough, we're specifically interested in 2 numbers after ,.

I think that if a library outputs 8s 255.28000000000065ms for 8255.28 input, it can be considered a bug. Can you please re-consider adding this?

EvanHahn commented 6 years ago

Good point—it's arguably a bug.

I'll think about this and get back to you.

EvanHahn commented 6 years ago

For future reference, let's discuss this in the PR at #140.