Rounding error in includes/ui/ui_view.inc -> price_in_words()
authorJoe Hunt <joe.hunt.consulting@gmail.com>
Tue, 7 Feb 2012 23:42:33 +0000 (00:42 +0100)
committerJoe Hunt <joe.hunt.consulting@gmail.com>
Tue, 7 Feb 2012 23:42:33 +0000 (00:42 +0100)
includes/ui/ui_view.inc

index 14d1eb021347967e630ac58a3d2bc76466563c8e..00c0cd0c9d64f3ad4f20d5b80c8beb47621e342d 100644 (file)
@@ -773,9 +773,15 @@ function price_in_words($amount, $document=0)
        $dec = user_price_dec();
        if ($dec > 0)
        {
-               $divisor = pow(10, $dec);       
-               $frac = round2($amount - floor($amount), $dec) * $divisor;
-               $frac = sprintf("%0{$dec}d", $frac);
+               $divisor = pow(10, $dec);
+               // algorithm rewritten due to a bug in sprintf (wrong with frac, 29, 57 and more).
+        $frac = round2($amount - floor($amount), $dec) * $divisor;
+        for ($div = $divisor / 10; $div > 1; $div /= 10)
+        {
+               if ($frac < $div)
+                       $frac = "0" . $frac;
+        }              
+               //$frac = sprintf("%0{$dec}d", $frac);
                $and = _("and");
        $frac = " $and $frac/$divisor";
     }