From: Joe Hunt Date: Tue, 7 Feb 2012 23:42:33 +0000 (+0100) Subject: Rounding error in includes/ui/ui_view.inc -> price_in_words() X-Git-Url: https://delta.frontaccounting.com/gitweb/?a=commitdiff_plain;h=70ddbd3a13e56b2cc5ce26e165e2b0dae138c273;p=textcart.git Rounding error in includes/ui/ui_view.inc -> price_in_words() --- diff --git a/includes/ui/ui_view.inc b/includes/ui/ui_view.inc index 14d1eb0..00c0cd0 100644 --- a/includes/ui/ui_view.inc +++ b/includes/ui/ui_view.inc @@ -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"; }