Fix function to convert price to string

This commit is contained in:
Laurent Destailleur 2020-08-06 02:48:16 +02:00
parent 9cb5735165
commit 73ea5b0e2d

View File

@ -13,37 +13,49 @@
* GNU General Public License for more details. * GNU General Public License for more details.
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
* or see http://www.gnu.org/ * or see https://www.gnu.org/
*/ */
/** /**
* \file htdocs/core/lib/functionsnumbertoword.lib.php * \file htdocs/core/lib/functionsnumtoword.lib.php
* \brief A set of functions for Dolibarr * \brief A set of functions for Dolibarr
* This file contains all frequently used functions. * This file contains all frequently used functions.
*/ */
/** /**
* Function to return number in text. * Function to return a number into a text.
* May use module NUMBERWORDS if found.
* *
* * @param float $num Number to convert (must be a numeric value, like reported by price2num())
* @param float $num Number to convert * @param Translate $langs Language
* @param Lang $langs Language
* @param boolean $currency 0=number to translate | 1=currency to translate * @param boolean $currency 0=number to translate | 1=currency to translate
* @param boolean $centimes 0=no centimes | 1=centimes to translate * @param boolean $centimes 0=no cents/centimes | 1=there is cents/centimes to translate
* @return string Text of the number * @return string|false Text of the number
*/ */
function dol_convertToWord($num, $langs, $currency = false, $centimes = false) function dol_convertToWord($num, $langs, $currency = false, $centimes = false)
{ {
global $conf; global $conf;
$num = str_replace(array(',', ' '), '', trim($num)); //$num = str_replace(array(',', ' '), '', trim($num)); This should be useless since $num MUST be a php numeric value
if (!$num) { if (!$num) {
return false; return false;
} }
if ($centimes && strlen($num) == 1) { if ($centimes && strlen($num) == 1) {
$num = $num * 10; $num = $num * 10;
} }
if (!empty($conf->global->MAIN_MODULE_NUMBERWORDS)) {
if ($currency) {
$type = 1;
} else {
$type = 0;
}
$concatWords = $langs->getLabelFromNumber($num, $type);
return $concatWords;
} else {
$TNum = explode('.', $num); $TNum = explode('.', $num);
$num = (int) $TNum[0]; $num = (int) $TNum[0];
$words = array(); $words = array();
@ -126,13 +138,18 @@ function dol_convertToWord($num, $langs, $currency=false, $centimes=false)
} }
// If we need to write cents call again this function for cents // If we need to write cents call again this function for cents
if(!empty($TNum[1])) { $decimalpart = $TNum[1];
$decimalpart = preg_replace('/0+$/', '', $decimalpart);
if ($decimalpart) {
if (!empty($currency)) $concatWords .= ' '.$langs->transnoentities('and'); if (!empty($currency)) $concatWords .= ' '.$langs->transnoentities('and');
$concatWords .= ' '.dol_convertToWord($TNum[1], $langs, $currency, true);
$concatWords .= ' '.dol_convertToWord($decimalpart, $langs, '', true);
if (!empty($currency)) $concatWords .= ' '.$langs->transnoentities('centimes'); if (!empty($currency)) $concatWords .= ' '.$langs->transnoentities('centimes');
} }
return $concatWords; return $concatWords;
} }
}
/** /**
@ -140,7 +157,7 @@ function dol_convertToWord($num, $langs, $currency=false, $centimes=false)
* *
* @deprecated * @deprecated
* @param float $numero Number to convert * @param float $numero Number to convert
* @param Lang $langs Language * @param Translate $langs Language
* @param string $numorcurrency 'number' or 'amount' * @param string $numorcurrency 'number' or 'amount'
* @return string Text of the number or -1 in case TOO LONG (more than 1000000000000.99) * @return string Text of the number or -1 in case TOO LONG (more than 1000000000000.99)
*/ */