Jump to content

Ratio class


Recommended Posts

Posted (edited)

LAST UPDATE: MARCH 10, 2015

package org.bjornkrols.math.ratio;

import java.math.BigDecimal;

/**
 * @author 		Bjorn Krols (Botre)
 * @version		0.0
 * @since		March 10, 2015
 */

public final class Ratio {
	
	private Ratio() {
		// This class should never be instantiated.
		// Do not delete or make accessible.
	}
	
	/*
	 * INT
	 */
	
	public static int percentage(int part, int whole) {
		return part * 100 / whole ;
	}
	
	/*
	 * LONG
	 */
	
	public static long percentage(long part, long whole) {
		return part * 100 / whole ;
	}
	
	/*
	 * FLOAT
	 */
	
	public static float percentage(float part, float whole) {
		return part * 100 / whole ;
	}
	
	public static float decimal(float part, float whole) {
		return part / whole ;
	}
	
	/*
	 * DOUBLE
	 */
	
	public static double percentage(double part, double whole) {
		return part * 100 / whole ;
	}
	
	public static double decimal(double part, double whole) {
		return part / whole ;
	}
	
	/*
	 * BIG DECIMAL
	 */
	
	private static final BigDecimal HUNDRED = new BigDecimal(100);
	
	public static BigDecimal percentage(BigDecimal part, BigDecimal whole) {
		part.multiply(HUNDRED).divide(whole);
	}
	
	public static BigDecimal decimal(BigDecimal part, BigDecimal whole) {
		return part.divide(whole);
	}
	
}
Edited by Botre
  • Like 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...