Jump to content

Ratio class


Botre

Recommended Posts

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
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

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