LoslegenKostenlos loslegen

Using math classes

Classes from java.math can be a bit more difficult to work with, but offer the ability to work with larger numbers and are more precise. In this exercise, you'll see the precision difference between a double and a BigDecimal from java.math.

Diese Übung ist Teil des Kurses

Data Types and Exceptions in Java

Kurs anzeigen

Anleitung zur Übung

  • Import the BigDecimal class from java.math.
  • Create an instance of BigDecimal to hold the number 9.123456789.
  • Multiply the BigDecimal in variable bigDec by ten.
  • Divide the BigDecimal in variable bigDec by hundred.

Interaktive Übung

Vervollständige den Beispielcode, um diese Übung erfolgreich abzuschließen.

// Import BigDecimal
import ___.___.___;

public class HelloWorld {

	public static void main(String[] args) {
		double d = 9.123456789;
		// Create a BigDecimal with the value of 9.123456789
		BigDecimal bigDec = ____ ____(____);
		BigDecimal ten = new BigDecimal(10);
		BigDecimal hundred = new BigDecimal(100);
		d = d*10;
		d = d/100;
		// Multiply bigDec by ten
		bigDec = ____.____(____);
		// Divide bigDec by hundred
		bigDec = ____.____(____);
		System.out.println(d);
		System.out.println(bigDec);
	}
}
Code bearbeiten und ausführen