Java – Math.pow example

A simple Math.pow example, display 2 to the power of 8.


In Math
2^8 = 2x2x2x2x2x2x2x2 =256

In Java
Math.pow(2, 8)
Note
In Java, the symbol ^ is a XOR operator, DON’T use this for the power calculation.
TestPower.java

package com.mkyong.test;

import java.text.DecimalFormat;

public class TestPower {

	static DecimalFormat df = new DecimalFormat(".00");
	
	public static void main(String[] args) {

		//1. Math.pow returns double, need cast, display 256
		int result = (int) Math.pow(2, 8);
		System.out.println("Math.pow(2, 8) : " + result);
		
		//2. Wrong, ^ is a binary XOR operator, display 10
		int result2 = 2 ^ 8;
		System.out.println("2 ^ 8 : " + result2);
		
		//3. Test double , display 127628.16
		double result3 = Math.pow(10.5, 5);
		System.out.println("Math.pow(10.5, 5) : " + df.format(result3));
		
	}

}

Output


Math.pow(2, 8) : 256
2 ^ 8 : 10
Math.pow(10.5, 5) : 127628.16

References

  1. Math.pow JavaDoc
  2. Wikipedia Exclusive or (XOR)

3 comments on “Java – Math.pow example

  1. package lib;

    public class MyNumFormat
    {
    public static double pow(double base, int exponent)
    {
    double base1 = 1;
    for(int i = 0; i < exponent; i++)
    {
    base1 *= base;
    }
    return base1;
    }
    /**
    * round – takes in a real # and rounds it up or down to a whole number.
    *
    * @param val
    * – the value to round up or down.
    * @return a whole number representing the rounded value.
    */
    public static int round(double num)
    {
    return (int)(num + 0.5);
    }
    /**
    * roundXDigits – rounds a number a certain number of places after the
    * decimal.
    *
    * @param val
    * – the value to round
    * @param place
    * – the # of places after the decimal that gets rounded up or
    * down
    * @return the real # rounded up/down in the correct place.
    */
    public static double roundXDigits(double num, int place)
    {
    double tenPow = pow(10, place);
    return round(num * tenPow) / tenPow;
    }
    /**
    * dollarsToMoney – represents a real# as a money string
    *
    * @param val
    * – the real# representing dollars and cents
    * @return a String in the format of $#.##
    */
    public static String dollarsToMoney(double val)
    {
    String start = "$";
    start += val;
    return start;
    }
    /**
    * centsToMoney – uses the dollarsToMoney function to change 153 cents to
    * $1.53
    *
    * @param val
    * – the number of pennies given
    * @return the money String represented as $#.## as outlined in the
    * dollarsToMoney function.
    */

    public static String centsToMoney(double val)
    package lib;

    public class MyNumFormat
    {
    public static double pow(double base, int exponent)
    {
    double base1 = 1;
    for(int i = 0; i < exponent; i++)
    {
    base1 *= base;
    }
    return base1;
    }
    /**
    * round – takes in a real # and rounds it up or down to a whole number.
    *
    * @param val
    * – the value to round up or down.
    * @return a whole number representing the rounded value.
    */
    public static int round(double num)
    {
    return (int)(num + 0.5);
    }
    /**
    * roundXDigits – rounds a number a certain number of places after the
    * decimal.
    *
    * @param val
    * – the value to round
    * @param place
    * – the # of places after the decimal that gets rounded up or
    * down
    * @return the real # rounded up/down in the correct place.
    */
    public static double roundXDigits(double num, int place)
    {
    double tenPow = pow(10, place);
    return round(num * tenPow) / tenPow;
    }
    /**
    * dollarsToMoney – represents a real# as a money string
    *
    * @param val
    * – the real# representing dollars and cents
    * @return a String in the format of $#.##
    */
    public static String dollarsToMoney(double val)
    {
    String start = "$";
    start += val;
    return start;
    }
    /**
    * centsToMoney – uses the dollarsToMoney function to change 153 cents to
    * $1.53
    *
    * @param val
    * – the number of pennies given
    * @return the money String represented as $#.## as outlined in the
    * dollarsToMoney function.
    */

    public static String centsToMoney(double val)
    {
    val /= 100;
    return dollarsToMoney(val);
    }
    }

    Reply

Leave a Comment

Your email address will not be published. Required fields are marked *