Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Unreleased

- fix: preserve decimal digits when truncating with `maxDecimalPoints`

# 3.35.0 / 2026-09-10

- new: Azerbaijani support (`az`)
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,7 @@ Lovingly made by [Evan Hahn](https://evanhahn.com/) with help from:
- [Kristijan Jesenski](https://github.com/kjesenski) for Slovenian support
- [Michal Karzel](https://github.com/Misioka) for improving Arabic support
- [Mikias Menjeta](https://github.com/OMikiasO) for Amharic support
- [Huanyi Xie](https://github.com/xiehuanyi) for fixing decimal truncation

Licensed under the permissive [Unlicense](https://unlicense.org/). Enjoy!

Expand Down
3 changes: 2 additions & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
"Michal Karzel (https://github.com/Misioka)",
"Batmend Ganbaatar (https://github.com/theironbatka)",
"Mikias Menjeta (https://github.com/OMikiasO)",
"Marko Plaznik (https://github.com/ohicee)"
"Marko Plaznik (https://github.com/ohicee)",
"Huanyi Xie (https://github.com/xiehuanyi)"
],
"version": "3.35.0",
"description": "Convert millisecond durations to English and many other languages.",
Expand Down
20 changes: 18 additions & 2 deletions humanize-duration.js
Original file line number Diff line number Diff line change
Expand Up @@ -1330,6 +1330,23 @@
throw new Error("No language found.");
}

/**
* @internal
* @param {number} count
* @param {number} decimalPoints
* @returns {number}
*/
function truncateCount(count, decimalPoints) {
var parts = count.toString().split("e");
var shifted = Number(
parts[0] + "e" + ((Number(parts[1]) || 0) + decimalPoints)
);
if (!isFinite(shifted)) return count;

parts = Math.floor(shifted).toString().split("e");
return Number(parts[0] + "e" + ((Number(parts[1]) || 0) - decimalPoints));
}

/**
* @internal
* @param {Piece} piece
Expand Down Expand Up @@ -1366,8 +1383,7 @@
var normalizedUnitCount =
maxDecimalPoints === void 0
? unitCount
: Math.floor(unitCount * Math.pow(10, maxDecimalPoints)) /
Math.pow(10, maxDecimalPoints);
: truncateCount(unitCount, maxDecimalPoints);
var countStr = normalizedUnitCount.toString();

if (language._hideCountIf2 && normalizedUnitCount === 2) {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
"Michal Karzel (https://github.com/Misioka)",
"Batmend Ganbaatar (https://github.com/theironbatka)",
"Mikias Menjeta (https://github.com/OMikiasO)",
"Marko Plaznik (https://github.com/ohicee)"
"Marko Plaznik (https://github.com/ohicee)",
"Huanyi Xie (https://github.com/xiehuanyi)"
],
"version": "3.35.0",
"description": "Convert millisecond durations to English and many other languages.",
Expand Down
14 changes: 14 additions & 0 deletions test/humanizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,20 @@ describe("humanizer", function () {
assert.strictEqual(h(7999), "7.999 seconds");
});

it('preserves decimal digits when truncating with "maxDecimalPoints"', function () {
const h = humanizer({ units: ["s"], maxDecimalPoints: 2 });

assert.strictEqual(h(1150), "1.15 seconds");
assert.strictEqual(h(2300), "2.3 seconds");
assert.strictEqual(h(1159), "1.15 seconds");
assert.strictEqual(
h(0.00115, { maxDecimalPoints: 8 }),
"0.00000115 seconds"
);
assert.strictEqual(h(1150, { maxDecimalPoints: 309 }), "1.15 seconds");
assert.strictEqual(h(1.15e24), "1.15e+21 seconds");
});

it('pluralizes based on the count rendered by "maxDecimalPoints"', function () {
const h = humanizer({ maxDecimalPoints: 0 });

Expand Down