top of page

When performing mathematical calculations, achieving the desired amount of precision is often important. Sticky situations can arise, however, when doing calculations with different variable types. Consider the following scenario:

 

int nNum1 = 7;

int nNum2 = 2;

double dNum3 = 3.3;

 

Think about what the result of each of these calculations would be:

 

nNum1 + dNum3                // Is this equivalent to 10.3 or 10?

nNum1 / nNum2       // Is this equivalent to 3.5 or 3?

 

Whenever integer and double (decimal) values are mixed in an expression, the double values are overriding so that the result is always a double value. Furthermore, if different

numerical types are mixed, the result will match that of the most precise type.

 

Type Casting

 

Sometimes in the course of a program, there is the need to convert a numerical value to a different (but compatible) type. For example, I might want to take 4.58 and convert it to its integer equivalent of 4. This conversion is called type casting. Here are a few examples of type casting:

 

int nNum;

double dNum = 4.56;

int nRad = 6;

double dRad;

 

nNum = (int)dNum;         /* converts the double variable dNum to an integer and
                             stores it in nNum, nNum = 4 */

dRad = (double)nRad;      /* converts the integer variable nRad to a double by                               adding a .0 to the end and stores it in dRad,

                             dRad = 6.0 */

 

Type casting can be done during a calculation, if necessary:

 

int nSum;

double dA = 1.2, dB = 5.7;

 

nSum = (int)(dA + dB);       //converts the sum to an integer

 

Rounding

 

There are a lot of clever ways to round values. I will highlight a few here, but realize that the same effect of rounding can be achieved in other interesting ways! In fact, if you’re up to the challenge try to find a way to round any double value to a specified decimal place just by using simple math operations and type casting!

 

(A) Math.round

 
1  public class Example {

2

3     public static void main(String[] args) {

4        double dA = 5.643;

5        dA = dA * 100;

6        dA = Math.round(dA);

7        dA = dA / 100;

8        System.out.println("The amount is " + dA );

9     }

10 }

 

This method is perhaps the simplest method, but requires a little bit of math “trickery”. Math.round takes a double value and rounds it to the nearest whole number. This is a problem if you want to round to 2 decimal places. The way around this is to first multiply your value by 100 (this shifts the decimal place 2 spots to the right). Then use Math.round to round to the nearest whole number. Then divide your value by 100 to move the decimal place back to its original position. Voila!

 

(B) NumberFormat Class

 

1  import java.text.NumberFormat;
2  public class Example {

3

4     public static void main(String[] args) {

5        double dRad = 5.643;

6        NumberFormat nf = NumberFormat.getNumberInstance();

7        nf.setMaximumFractionDigits(2);

8        nf.setMinimumFractionDigits(2);

9        System.out.println("The radius is "+ nf.format(dRad) );

10    }

11 }


 

Line 1: This imports the NumberFormat package (needed to utilize the NumberFormat class) and must be done above the public class

Line 6: Declares a new formatting variable, called ‘nf’ which can be used to format numbers. Use this exact syntax, but you can call the formatting variable whatever you want, it doesn’t have to be ‘nf’.

Line 7 & 8: Sets the maximum and minimum digits to be displayed on numbers formatted by nf, in this case, it is set at exactly 2 decimal places

Line 9: When printing the value of dRad to the output window, it is “surrounded” by the nf.format() method. This makes the value of dRad abide by the rules we’ve already set for nf. 

 

(C) DecimalFormat Class

 

1   import java.text.*;

2   public class Example {

3  

4      public static void main(String[] args) {

5         DecimalFormat twoDec = new DecimalFormat("#0.00");

6         double dAmt = 78.456;

7         String sAmt= twoDec.format(dAmt);

8         System.out.println("The amount is " + sAmt);

9      }

10  }

 

Line 1: This imports the whole text package (needed to utilize the DecimalFormat class) and must be done above the public class

Line 5: Declares a new formatting variable, called ‘twoDec’ which can be used to format numbers. It also sets the formatting structure to 2 decimal places → (“#0.00”)

Line 7: “Wraps” the dAmt variable in the formatting specified by twoDec. This rounded value should be stored in a String variable, in this case our String variable is called sAmt

Line 8: Print the String variable to the output window 

Type Casting & Rounding

bottom of page