Participer au site avec un Tip
Rechercher
 

Améliorations / Corrections

Vous avez des améliorations (ou des corrections) à proposer pour ce document : je vous remerçie par avance de m'en faire part, cela m'aide à améliorer le site.

Emplacement :

Description des améliorations :

Module : java.base - Package : java.lang - Classe : Math
Version documentée : Java SE 17

Méthode « Math.round »

Signature

public static long round( double value );

Description

Cette méthode renvoie l'arrondi entier au plus proche de la valeur spécifiée en paramètre.

Paramètre

ParamètreTypeDescription
value
double
La valeur à partir de laquelle calculer l'arrondi entier le plus proche.

Valeur de retour

La valeur de retour est de type long.

Exemple de code

 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 
public class Sample {
    
    public static void main( String[] args ) {
        
        double value = 4.7;
        System.out.printf( "Math.round( %.1f ) == %d\n", value, Math.round( value ) );
        
        value = 4.1;
        System.out.printf( "Math.round( %.1f ) == %d\n", value, Math.round( value ) );
        
        value = 4.0;
        System.out.printf( "Math.round( %.1f ) == %d\n", value, Math.round( value ) );
        
        value = 0;
        System.out.printf( "Math.round( %.1f ) == %d\n", value, Math.round( value ) );
        
        value = -1.3;
        System.out.printf( "Math.round( %.1f ) == %d\n", value, Math.round( value ) );
        
        value = -1.9;
        System.out.printf( "Math.round( %.1f ) == %d\n", value, Math.round( value ) );

        value = -2;
        System.out.printf( "Math.round( %.1f ) == %d\n", value, Math.round( value ) );
    }
    
}
Exemple d'utilisation de la méthode Math.round

Et voici les résultats produits par cet exemple de code.

Math.round( 4,7 ) == 5
Math.round( 4,1 ) == 4
Math.round( 4,0 ) == 4
Math.round( 0,0 ) == 0
Math.round( -1,3 ) == -1
Math.round( -1,9 ) == -2
Math.round( -2,0 ) == -2

Disponible depuis

1.0

Voir aussi

La méthode Math.ceil
La méthode Math.floor
Les fonctions round, roundf et roundl en C
La fonction round en Python