栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java常用工具类 数字工具类

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java常用工具类 数字工具类

本文实例为大家分享了java常用工具类,数字工具类的具体代码,供大家参考,具体内容如下

package com.jarvis.base.util;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Random;


public class NumericHelper {

 
 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;
 }

 
 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();
 }

 
 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();
 }

 
 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();
 }

 
 public static String format(double num) {
 return format(num, "0.00");
 }

 
 public static String format(double num, int digits) {
 String pattern = "0";
 if (digits > 0) {
 pattern += "." + createStr("0", digits, "");
 }
 return format(num, pattern);
 }

 
 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();
 }
 }

 
 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);
 }

 
 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;
 }

 
 public static String unit(double scale) {
 if (scale == 1 || scale == 0) {
 return "";// 不设置单位倍率单位,使用基准单位
 }
 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 "无量";
 }
 }

 
 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;
 }
 }

 
 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));
 }

 
 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();
 }

 
 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;
 }

 
 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();// 默认格式
 nbf3.setGroupingUsed(false);
 nbf3.setMinimumFractionDigits(points);
 nbf3.setMaximumFractionDigits(points);
 double scaledNum = number / scale;
 result = nbf3.format(scaledNum);
 if (flag) {
 result = "-" + result;
 }
 return result;
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/138102.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号