_bestPrefix now chooses shortest prefix when prefix values are the same

This commit is contained in:
Eric 2015-08-07 13:17:23 +00:00
parent 5e7a584ab1
commit 96aca3a7bb
2 changed files with 7 additions and 3 deletions

View File

@ -911,9 +911,12 @@ function factory (type, config, load, typed) {
var diff = Math.abs(
Math.log(absValue / prefix.value) / Math.LN10 - 1.2);
if (diff < bestDiff) {
bestPrefix = prefix;
bestDiff = diff;
if (diff < bestDiff
|| (diff === bestDiff && prefix.name.length < bestPrefix.name.length)) {
// choose the prefix with the smallest diff, or if equal, choose the one
// with the shortest name (can happen with SHORTLONG for example)
bestPrefix = prefix;
bestDiff = diff;
}
}
}

View File

@ -364,6 +364,7 @@ describe('unit', function() {
assert.equal(new Unit(500 ,'m').toString(), '500 m');
assert.equal(new Unit(600 ,'m').toString(), '0.6 km');
assert.equal(new Unit(1000 ,'m').toString(), '1 km');
assert.equal(new Unit(1000 ,'ohm').toString(), '1 kohm');
});