In this exercise, you will practice working with methods and BigDecimal
numbers.
bigAdd
that adds two int
s and produces a BigDecimal
.digitSum
that calculates the digit sum of a given BigDecimal
, returning an int
.digitsToWords
that converts an int
to a string consisting of the English words of its digits.main
method should look:
System.out.print("Adding 5 to Integer.MAX_VALUE: "); BigDecimal result = bigAdd(5, Integer.MAX_VALUE); System.out.println(result); System.out.print("Digit sum of " + result + ": "); int digitSum = digitSum(result); System.out.println(digitSum); System.out.print("Digits as words: "); String words = digitsToWords(digitSum); System.out.println(words);Program output:
Adding 5 to Integer.MAX_VALUE: 2147483652 Digit sum of 2147483652: 42 Digits as words: four two
equals
to check whether two BigDecimal
numbers are equal.remainder
method instead of %
to extract the last digit of a BigDecimal
.BigDecimal
, use number.divide(BigDecimal.TEN, RoundingMode.FLOOR)
.