|
|
|
|
Salut,
voilà qui pourrait t'aider // Methode arrondi //-------------- // Renvoie un reel avec 2 chiffres apres la virgule public double arrondi(double val) {return (Math.floor(val*100.0))/100;} Bon java puce
|
static public double arrondir(double value, int n) {
double r = (Math.round(value * Math.pow(10, n))) / (Math.pow(10, n)); return r; } static public Double arrondir(Double value, int n) { double r = (Math.round(value.doubleValue() * Math.pow(10, n))) / (Math.pow(10, n)); return new Double(r); } ==>Exemple d'utilisation : arrondir(new Double(0.004),2) ;//arrondi à la 2eme décimale |
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);
}
}
|
Résultats pour arrondi en java
Résultats pour arrondi en java
Résultats pour arrondi en java
Résultats pour arrondi en java
Résultats pour arrondi en java