1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| class OutilTagLib
{
String getFormateNombre(Double inNombre, Integer inNombreDecimales, Double inDefautNombre)
{
Double nombre = inNombre
String nombreString = ""
// nombre par defaut
if(!nombre)
{ if(inDefautNombre) { nombre = inDefautNombre } }
else
{ // nombre de decimales par defaut
if(!inNombreDecimales)
{ inNombreDecimales = 2 }
// ex : pour arrondir nombre au centieme, on le multiplie par 100,
// on arrondie a l'entier superieur (ceil) puis on redivise par 100
Double deca = Math.pow(10, inNombreDecimales)
nombre = (Double) Math.ceil(nombre * deca) / deca
// ex : pour formater au centieme, on donne 0.00 comme format
String formatDecimales = "0."
for(int i = 0; i < inNombreDecimales; ++i) { formatDecimales += "0" }
DecimalFormat df = new DecimalFormat(formatDecimales);
nombreString = df.format(nombre)
// ex : pour formater a la francaise, on remplace le "." par une ","
nombreString.replace('.', ',')
}
return nombreString
}
def formateNombre =
{ attrs, body ->
out << body() << getFormateNombre(attrs.nombre, attrs.decimales, attrs.defaut)
}
} |
Partager