1. 程式人生 > >JAVA實現EXCEL公式專題(五)——數字處理函式

JAVA實現EXCEL公式專題(五)——數字處理函式

由於數字處理已經是非常常見的了,比如四捨五入、取整、整除等等,在這裡不多說,上點簡單的程式碼

/**
 *  專案名稱:
 *  檔案說明:EXCEL公式型別:數字公式
 *  主要特點:
 *  版本號:1.0
 *  製作人:xxx
 *  建立時間:2013-12-3
 **/
package EXCEL;

import java.util.Random;

public class NumberFunctions {

	/**
	 * 簡單隨機數
	 * @return
	 */
	public static double rand()
	{
		return new Random().nextDouble();
	}


	/**
	 * 獲得從a到b的隨機數
	 * @param n
	 * @return
	 */
	public static int randomBetween(int a,int b)
	{
		return (int) (new Random().nextDouble()*(b-a+1)+a);
	}

	/**
	 * 四捨五入
	 * @param a
	 * @param pre
	 * @return
	 */
	public static double round(double a,int pre)
	{
		return Math.round(a*Math.pow(10, pre))/Math.pow(10, pre);
	}


	public static void main(String[] args) {
		/*******************測試數字*****************************/

		System.out.println("隨機數"+rand());
		System.out.println("1-10隨機整數"+randomBetween(1,10));
		System.out.println("四捨五入"+round(9.456,2));
	}
}