|
|
|
|
Bonjour,
Je souhaiterai arraondir un nombre de type "double" qui a un dizaine de chiffres après la virgule afin qu'il n'en est plus que 2 au maximum.
J'ai trouvé la fonction round mais cela provoque une erreur à la compilation "double cannot be dereferenced".
(J'utilise Java 1.4.1.)
Si vous pouviez m'aider
Merci par avance
Static public double arrondir(double value, int n) {
|
Empêche des erreurs
public class RoundTest
{
public static void main(String[] args)
{
double num = myRound(15.6666666, 2);
System.out.println( num ); //it’s a double
String str = myRound(15.6666666, "0.00");
System.out.println( str ); //it’s a String
}
private static double myRound(double value, int decimalPlaces)
{
if(decimalPlaces < 0) { return value; }
double augmentation = Math.pow(10, decimalPlaces);
return Math.round(value * augmentation) / augmentation;
}
private static String myRound(double value, String format)
{
if(format == null || format.length() <= 0) { return String.valueOf(value); }
return new DecimalFormat(format).format(value);
}
}
|