From dfafd8815915bd5c5997912366faf46937cafa41 Mon Sep 17 00:00:00 2001 From: Benjamin Renard Date: Tue, 7 Apr 2015 20:34:18 +0200 Subject: [PATCH] LSformElement :: valueWithUnit : improve floatint number display and add parameter for formatting control --- .../LSattr_html_valueWithUnit.docbook | 26 ++++++++++++++++- .../class.LSformElement_valueWithUnit.php | 29 +++++++++++++++---- 2 files changed, 48 insertions(+), 7 deletions(-) diff --git a/doc/conf/LSattribute/LSattr_html/LSattr_html_valueWithUnit.docbook b/doc/conf/LSattribute/LSattr_html/LSattr_html_valueWithUnit.docbook index bf565d3f..d3765129 100644 --- a/doc/conf/LSattribute/LSattr_html/LSattr_html_valueWithUnit.docbook +++ b/doc/conf/LSattribute/LSattr_html/LSattr_html_valueWithUnit.docbook @@ -10,7 +10,10 @@ '[facteur1]' => '[label unit1]', '[facteur2]' => '[label unit2]', [...] - ) + ), + 'nb_decimals' => [number of decimals], + 'dec_point' => '[decimals point]', + 'thousands_sep' => '[thousands separator]' ) ),]]> ... @@ -26,6 +29,27 @@ + + nb_decimals + + Le nombre de décimals à afficher en cas de nombre non-entier (Par defaut : 2). + + + + + dec_point + + Le caractère à utiliser comme séparateur de décimal (Par defaut, une virgule). + + + + + thousands_sep + + Le caractère à utiliser comme séparateur de milliers (Par defaut, un espace). + + + diff --git a/public_html/includes/class/class.LSformElement_valueWithUnit.php b/public_html/includes/class/class.LSformElement_valueWithUnit.php index 71bba0ca..caa6f94d 100644 --- a/public_html/includes/class/class.LSformElement_valueWithUnit.php +++ b/public_html/includes/class/class.LSformElement_valueWithUnit.php @@ -53,6 +53,25 @@ class LSformElement_valueWithUnit extends LSformElement { return; } + /** + * Return formatted number + * + * This method return take a number as paremeter and + * return it after formatting. + * + * @param[in] int|float $number The number + * + * @retbal string Formatted number + */ + function formatNumber($number) { + if ((int)$number==$number) return $number; + return number_format($number, + (isset($this -> params['html_options']['nb_decimals'])?$this -> params['html_options']['nb_decimals']:2), + (isset($this -> params['html_options']['dec_point'])?$this -> params['html_options']['dec_point']:","), + (isset($this -> params['html_options']['thousands_sep'])?$this -> params['html_options']['thousands_sep']:" ") + ); + } + /** * Retourne les infos d'affichage de l'élément * @@ -74,12 +93,10 @@ class LSformElement_valueWithUnit extends LSformElement { ); foreach($units as $sill => $label) { if ($infos['value'] >= $sill) { - if ($infos['value'] % $sill == 0) { - $infos['valueWithUnit']=$infos['value']/$sill; - $infos['unitSill']=$sill; - $infos['unitLabel']=$label; - break; - } + $infos['valueWithUnit']=$this -> formatNumber($infos['value']/$sill); + $infos['unitSill']=$sill; + $infos['unitLabel']=$label; + break; } } $values_and_units[$value] = $infos;