0
0
mirror of https://github.com/ezyang/htmlpurifier.git synced 2024-12-22 08:21:52 +00:00

Update UnitConverter to deal more correctly with X.XX... decimals. Not complete.

git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@1749 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
Edward Z. Yang 2008-05-20 17:48:15 +00:00
parent fc7dbdbd33
commit fda310f1e7
2 changed files with 20 additions and 6 deletions

View File

@ -149,12 +149,18 @@ class HTMLPurifier_UnitConverter
// Calculations will always be carried to the decimal; this is
// a limitation with BC (we can't set the scale to be negative)
$new_log = (int) floor(log($n, 10));
$rp = $sigfigs - $new_log - $log - 1;
if ($rp < 0) $rp = 0;
$n = bcadd($n, '0.' . str_repeat('0', $rp) . '5', $rp + 1);
$n = bcdiv($n, '1', $rp);
$rp = $sigfigs - $new_log - 1;
//echo "----\n";
//echo "$n\nsigfigs = $sigfigs\nnew_log = $new_log\nlog = $log\nrp = $rp\n";
if ($rp >= 0) {
$n = bcadd($n, '0.' . str_repeat('0', $rp) . '5', $rp + 1);
$n = bcdiv($n, '1', $rp);
} else {
if ($new_log + 1 >= $sigfigs) {
$n = bcadd($n, '5' . str_repeat('0', $new_log - $sigfigs));
$n = substr($n, 0, $sigfigs) . str_repeat('0', $new_log + 1 - $sigfigs);
}
}
if (strpos($n, '.') !== false) $n = rtrim($n, '0');
$n = rtrim($n, '.');

View File

@ -39,4 +39,12 @@ class HTMLPurifier_UnitConverterTest extends HTMLPurifier_Harness
$this->assertConversion('0.3937in', '1cm');
}
function testRounding() {
$this->assertConversion('100pt', '1.389in');
$this->assertConversion('1000pt', '13.89in');
$this->assertConversion('10000pt', '138.9in');
$this->assertConversion('100000pt', '1389in');
$this->assertConversion('1000000pt', '13890in');
}
}