java常用工具類 數(shù)字工具類
更新時(shí)間:2019年05月30日 09:27:20 作者:遠(yuǎn)方©
這篇文章主要為大家詳細(xì)介紹了java常用工具類中的數(shù)字工具類,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了java常用工具類,數(shù)字工具類的具體代碼,供大家參考,具體內(nèi)容如下
package com.jarvis.base.util;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Random;
public class NumericHelper {
/**
* 描述:通過一個(gè)整數(shù)i獲取你所要的哪幾個(gè)(從0開始) i為 多個(gè)2的n次方之和,如i=7,那么根據(jù)原值是2的n次方之各,你的原值必定是1,2,4 。
*
* @param i
* 數(shù)值
* @return
*/
public static int[] getWhich(long i) {
int exp = Math.getExponent(i);
if (i == (1 << (exp + 1)) - 1) {
exp = exp + 1;
}
int[] num = new int[exp];
int x = exp - 1;
for (int n = 0; (1 << n) < i + 1; n++) {
if ((1 << (n + 1)) > i && (1 << n) < (i + 1)) {
num[x] = n;
i -= 1 << n;
n = 0;
x--;
}
}
return num;
}
/**
* 描述:非四舍五入取整處理
*
* @param v
* 需要四舍五入的數(shù)字
* @return
*/
public static int roundDown(double v) {
BigDecimal b = new BigDecimal(Double.toString(v));
BigDecimal one = new BigDecimal("1");
return b.divide(one, 0, BigDecimal.ROUND_DOWN).intValue();
}
/**
* 描述:四舍五入取整處理
*
* @param v
* 需要四舍五入的數(shù)字
* @return
*/
public static int roundUp(double v) {
BigDecimal b = new BigDecimal(Double.toString(v));
BigDecimal one = new BigDecimal("1");
return b.divide(one, 0, BigDecimal.ROUND_UP).intValue();
}
/**
* 描述:提供精確的小數(shù)位四舍五入處理。
*
* @param v
* 需要四舍五入的數(shù)字
* @param scale
* 小數(shù)點(diǎn)后保留幾位
* @return 四舍五入后的結(jié)果
*/
public static double round(double v, int scale) {
if (scale < 0) {
throw new IllegalArgumentException("The scale must be a positive integer or zero");
}
BigDecimal b = new BigDecimal(Double.toString(v));
BigDecimal one = new BigDecimal("1");
return b.divide(one, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
}
/**
* 描述:四舍五入保留兩位小數(shù)
*
* @param num
* 數(shù)字
* @return 保留兩位小數(shù)的數(shù)字字串
*/
public static String format(double num) {
return format(num, "0.00");
}
/**
* 描述:四舍五入數(shù)字保留小數(shù)位
*
* @param num
* 數(shù)字
* @param digits
* 小數(shù)位
* @return
*/
public static String format(double num, int digits) {
String pattern = "0";
if (digits > 0) {
pattern += "." + createStr("0", digits, "");
}
return format(num, pattern);
}
/**
* 描述:生成字符串
*
* @param arg0
* 字符串元素
* @param arg1
* 生成個(gè)數(shù)
* @param arg2
* 間隔符號
* @return
*/
private static String createStr(String arg0, int arg1, String arg2) {
if (arg0 == null) {
return "";
} else {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < arg1; i++) {
if (arg2 == null)
arg2 = "";
sb.append(arg0).append(arg2);
}
if (sb.length() > 0) {
sb.delete(sb.lastIndexOf(arg2), sb.length());
}
return sb.toString();
}
}
/**
* 描述:數(shù)字格式化
*
* @param num
* 數(shù)字
* @param pattern
* 格式
* @return
*/
public static String format(double num, String pattern) {
NumberFormat fmt = null;
if (pattern != null && pattern.length() > 0) {
fmt = new DecimalFormat(pattern);
} else {
fmt = new DecimalFormat();
}
return fmt.format(num);
}
/**
* 求浮點(diǎn)數(shù)的權(quán)重
*
* @param number
* @return
*/
public static double weight(double number) {
if (number == 0) {
return 1;
}
double e = Math.log10(Math.abs(number));
int n = Double.valueOf(Math.floor(e)).intValue();
double weight = 1;
if (n > 0) {
for (int i = 0; i < n; i++) {
weight *= 10;
}
} else {
for (int i = 0; i > n; i--) {
weight /= 10;
}
}
return weight;
}
/**
* 獲得權(quán)重的單位
*
* @param scale
* @return
*/
public static String unit(double scale) {
if (scale == 1 || scale == 0) {
return "";// 不設(shè)置單位倍率單位,使用基準(zhǔn)單位
}
String[] units = new String[] { "十", "百", "千", "萬", "十萬", "百萬", "千萬", "億", "十億", "百億", "千億", "兆" };
String[] units2 = new String[] { "十分", "百分", "千分", "萬分", "十萬分", "百萬分", "千萬分" };
double e = Math.log10(scale);
int position = Double.valueOf(Math.ceil(e)).intValue();
if (position >= 1 && position <= units.length) {
return units[position - 1];
} else if (position <= -1 && -position <= units2.length) {
return units2[-position - 1];
} else {
return "無量";
}
}
/**
* 獲得浮點(diǎn)數(shù)的縮放比例
*
* @param num
* @return
*/
public static double scale(double num) {
double absValue = Math.abs(num);
// 無需縮放
if (absValue < 10000 && absValue >= 1) {
return 1;
}
// 無需縮放
else if (absValue < 1 && absValue > 0.0001) {
return 1;
} else {
return weight(num) / 10;
}
}
/**
* 獲得縮放后并且格式化的浮點(diǎn)數(shù)
*
* @param num
* @param scale
* @return
*/
public static double scaleNumber(double num, double scale) {
DecimalFormat df = null;
if (scale == 1) {
df = new DecimalFormat("#.0000");
} else {
df = new DecimalFormat("#.00");
}
double scaledNum = num / scale;
return Double.valueOf(df.format(scaledNum));
}
/**
* 產(chǎn)生n位隨機(jī)數(shù) TODO:性能不要,有待優(yōu)化
*/
public static String ramdomNumber(int n) {
if (n <= 0) {
throw new IllegalArgumentException("n must be positive !");
}
Random random = new Random();
StringBuilder result = new StringBuilder();
for (int i = 0; i < n; i++) {
result.append(random.nextInt(10));
}
return result.toString();
}
/**
* 縮放1W倍
*/
public static double changeTo(double number) {
boolean flag = false;
if (number < 0) {
flag = true;
}
double value = Math.abs(number);
value = value / 10000.0;
if (flag) {
value = Double.parseDouble("-" + value);
}
return value;
}
/**
*
* 描述:縮放比例
*
* @param number
* @param scale
* @param points
* @return
*/
public static String scaleNumberToStr(double number, double scale, int points) {
boolean flag = (number < 0);
number = Math.abs(number);
String result = "";
DecimalFormat nbf3 = (DecimalFormat) NumberFormat.getInstance();// 默認(rèn)格式
nbf3.setGroupingUsed(false);
nbf3.setMinimumFractionDigits(points);
nbf3.setMaximumFractionDigits(points);
double scaledNum = number / scale;
result = nbf3.format(scaledNum);
if (flag) {
result = "-" + result;
}
return result;
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
解析探秘fescar分布式事務(wù)實(shí)現(xiàn)原理
這篇文章主要為大家解析探秘fescar分布式事務(wù)的實(shí)現(xiàn)原理,fescar的txc模型實(shí)現(xiàn)非常有研究的價(jià)值,所以今天我們來好好翻一翻fescar項(xiàng)目的代碼2022-02-02
springboot 啟動(dòng)時(shí)初始化數(shù)據(jù)庫的步驟
這篇文章主要介紹了springboot 啟動(dòng)時(shí)初始化數(shù)據(jù)庫的步驟,幫助大家更好的理解和使用springboot框架,感興趣的朋友可以了解下2021-01-01
動(dòng)態(tài)代理模擬實(shí)現(xiàn)aop的示例
下面小編就為大家?guī)硪黄獎(jiǎng)討B(tài)代理模擬實(shí)現(xiàn)aop的示例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧,希望對大家有所幫助2017-11-11
Android開發(fā)中實(shí)現(xiàn)用戶注冊和登陸的代碼實(shí)例分享
這篇文章主要介紹了Android開發(fā)中實(shí)現(xiàn)用戶注冊和登陸的代碼實(shí)例分享,只是實(shí)現(xiàn)基本功能,界面華麗度就請忽略啦XD 需要的朋友可以參考下2015-12-12
Mybatis批量更新對象數(shù)據(jù)的兩種實(shí)現(xiàn)方式
這篇文章主要介紹了Mybatis批量更新對象數(shù)據(jù)的兩種實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08
JavaWeb基于Session實(shí)現(xiàn)的用戶登陸注銷方法示例
為了安全起見,session常常用來保存用戶的登錄信息。那么服務(wù)器是怎么來實(shí)現(xiàn)的呢?下面這篇文章就來給大家介紹了關(guān)于JavaWeb基于Session實(shí)現(xiàn)的用戶登陸注銷的相關(guān)資料,需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-12-12

