Dies ist wohl einer der top100 genutzten Funktionen, die nicht nur für eine KI sinnvoll ist, sondern auch bei Zahlen mit vielen stellen nach dem komma nützlich ist.

Die Ausgabe des gezeigten Beispiel ist:

kmRound(1.515, 2):1.5
mathRound(1.515, 2):1.5

/*
 * Beispiele erstellt von Emanuel Seibold
 * Quelle EBooks, Tutorials
 */
package tutorialzahlen;
 
public class Main {
 
    public static void main(String[] args) {
        System.out.println("kmRound(1.515, 2):" + String.valueOf(kmRound(1.51, 1)));
        System.out.println("mathRound(1.515, 2):" + String.valueOf(mathRound(1.51, 1)));
 
    }
 
    /* kaufmännisches runden */
    public static double kmRound(double nr, int n) {
        return (Math.round(nr * Math.pow(10, n))) / Math.pow(10, n);
    }
 
    /* mathematisches runden */
    public static double mathRound(double nr, int n) {
        return (Math.rint(nr * Math.pow(10, n))) / Math.pow(10, n);
    }
}