LSformElement :: valueWithUnit : improve floatint number display and add parameter for formatting control

This commit is contained in:
Benjamin Renard 2015-04-07 20:34:18 +02:00
parent 91dbcf4a6e
commit dfafd88159
2 changed files with 48 additions and 7 deletions

View file

@ -10,7 +10,10 @@
'[facteur1]' => '[label unit1]', '[facteur1]' => '[label unit1]',
'[facteur2]' => '[label unit2]', '[facteur2]' => '[label unit2]',
[...] [...]
) ),
'nb_decimals' => [number of decimals],
'dec_point' => '[decimals point]',
'thousands_sep' => '[thousands separator]'
) )
),]]> ),]]>
... ...
@ -26,6 +29,27 @@
</listitem> </listitem>
</varlistentry> </varlistentry>
<varlistentry>
<term>nb_decimals</term>
<listitem>
<simpara>Le nombre de décimals à afficher en cas de nombre non-entier (Par defaut : <literal>2</literal>).</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>dec_point</term>
<listitem>
<simpara>Le caractère à utiliser comme séparateur de décimal (Par defaut, une virgule).</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>thousands_sep</term>
<listitem>
<simpara>Le caractère à utiliser comme séparateur de milliers (Par defaut, un espace).</simpara>
</listitem>
</varlistentry>
</variablelist> </variablelist>
</sect4> </sect4>

View file

@ -53,6 +53,25 @@ class LSformElement_valueWithUnit extends LSformElement {
return; 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 * Retourne les infos d'affichage de l'élément
* *
@ -74,12 +93,10 @@ class LSformElement_valueWithUnit extends LSformElement {
); );
foreach($units as $sill => $label) { foreach($units as $sill => $label) {
if ($infos['value'] >= $sill) { if ($infos['value'] >= $sill) {
if ($infos['value'] % $sill == 0) { $infos['valueWithUnit']=$this -> formatNumber($infos['value']/$sill);
$infos['valueWithUnit']=$infos['value']/$sill; $infos['unitSill']=$sill;
$infos['unitSill']=$sill; $infos['unitLabel']=$label;
$infos['unitLabel']=$label; break;
break;
}
} }
} }
$values_and_units[$value] = $infos; $values_and_units[$value] = $infos;