com.centit.support.algorithm.NumberBaseOpt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of centit-utils Show documentation
Show all versions of centit-utils Show documentation
java 常用工具类,作为 apache-commons的补充
package com.centit.support.algorithm;
import java.math.BigDecimal;
import java.math.BigInteger;
@SuppressWarnings("unused")
public abstract class NumberBaseOpt {
private NumberBaseOpt() {
throw new IllegalAccessError("Utility class");
}
/*
* 获得某一位上的数值,如果 nBit<0 则获得小数点后面的位数
*/
static public char getNumByte(String szNum , int nBit)
{
int sl = szNum.length();
int nPos = 0;
while(nPos= sl) return '0';
return szNum.charAt(nPos);
}
final static private String CNum[]={"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
final static private String CNum2[]={"〇","一","二","三","四","五","六","七","八","九"};
final static private String CBit[]={"","拾","佰","仟"};
//拾佰仟万拾佰仟亿拾佰仟萬
/*
* 将数值大写
*/
public static String capitalization(String szNum)
{
StringBuilder resstr = new StringBuilder();
String tmpstr = szNum.trim();
int sl = tmpstr.length();
int sp=0;
int dotpos = tmpstr.indexOf('.');
if(dotpos != -1){
while(sl>1 && tmpstr.charAt(sl-1) == '0') sl--;
if(tmpstr.charAt(sl-1)=='.') sl--;
if(sl != tmpstr.length()){
tmpstr = tmpstr.substring(0,sl);
}
}else dotpos = sl;
if(sl<1) return CNum[0];
if(tmpstr.charAt(0) == '-'){
resstr.append("负");
sp = 1;
}
String integerNum = tmpstr.substring(sp,dotpos-sp);
String decimalNum ="";
if(dotpos+1 0) integerNum = integerNum.substring(sp);
int inl = integerNum.length();
if(inl>0){
int h = (inl-1) % 4 ;
int j = (inl-1) / 4 + 1;
sp=0;
boolean allzero = false;
boolean preallzero = false;
for(;j>0;j--){
int k=h;
h = 3;
boolean preiszero = allzero;
allzero = true;
for(;k>=0;k--,sp++){
if(integerNum.charAt(sp) == '0')
preiszero = true;
else{
allzero = false;
if(preiszero)
resstr.append("零");
preiszero = false;
resstr.append(CNum[(byte) (integerNum.charAt(sp)) - 48]).append(CBit[k]);
}
}// end for k
if(/*j!=0 &&*/ j % 2 == 0 ){
if(!allzero)
resstr.append("万");
}
else
{
if(!allzero || !preallzero){
int repyi = j/2;
for(int i=0; i0){
resstr.append("点");
for(int i=0; i='0' && tmpstr.charAt(sp)<='9')
resstr.append(CNum2[tmpstr.charAt(sp)-'0']);
else
resstr.append(tmpstr.charAt(sp));
return resstr.toString();
}
public static String capitalization(String szNum,final boolean isSimple)
{
if (isSimple){
return uppercaseCN(szNum);
}
return capitalization(szNum);
}
/*
* 这个仅仅是对Long.parseLong进行简单的封装避免重复的输入try catch
*/
static public Long parseLong(String sNum, Long errorValue){
Long lValue;
try{
lValue = Long.parseLong(sNum);
}catch(NumberFormatException e){
lValue = errorValue;
}
return lValue;
}
static public Long parseLong(String sNum){
return parseLong(sNum,null);
}
/*
* 这个仅仅是对Integer.parseInteger进行简单的封装避免重复的输入try catch
*/
static public Integer parseInteger(String sNum, Integer errorValue){
Integer lValue;
try{
lValue = Integer.parseInt(sNum);
}catch(NumberFormatException e){
lValue = errorValue;
}
return lValue;
}
/*
* 这个仅仅是对Integer.parseInteger进行简单的封装避免重复的输入try catch
*/
static public Integer parseInteger(String sNum){
return parseInteger(sNum,null);
}
/*
* 这个仅仅是对Double.parseDouble进行简单的封装避免重复的输入try catch
*/
public static Double parseDouble(String sNum, Double errorValue){
Double lValue;
try{
lValue = Double.parseDouble(sNum);
}catch(NumberFormatException e){
lValue = errorValue;
}
return lValue;
}
/*
* 这个仅仅是对Float.parseDouble进行简单的封装避免重复的输入try catch
*/
public static Float parseFloat(String sNum, Float errorValue){
Float lValue;
try{
lValue = Float.parseFloat(sNum);
}catch(NumberFormatException e){
lValue = errorValue;
}
return lValue;
}
static public Double parseDouble(String sNum){
return parseDouble(sNum,null);
}
/*
* 将一个Object转换为 long
*/
public static Long castObjectToLong(Object obj){
if (obj == null)
return null;
if (obj instanceof Long)
return (Long) obj;
if (obj instanceof Double)
return ((Double) obj).longValue();
if (obj instanceof Integer)
return ((Integer) obj).longValue();
if (obj instanceof Float)
return ((Float) obj).longValue();
if (obj instanceof String)
return parseLong((String)obj,null);
if (obj instanceof Number)
return ((Number) obj).longValue();
return parseLong(StringBaseOpt.objectToString(obj),null);
}
public static Long castObjectToLong(Object obj, Long defaultValue){
return GeneralAlgorithm.nvl(castObjectToLong(obj),defaultValue);
}
public static Integer castObjectToInteger(Object obj){
if (obj == null)
return null;
if (obj instanceof Integer)
return (Integer) obj;
if (obj instanceof Long)
return ((Long) obj).intValue();
if (obj instanceof Double)
return ((Double) obj).intValue();
if (obj instanceof Float)
return ((Float) obj).intValue();
if (obj instanceof String)
return parseInteger((String)obj,null);
if (obj instanceof Number)
return ((Number) obj).intValue();
return parseInteger(StringBaseOpt.objectToString(obj),null);
}
public static Integer castObjectToInteger(Object obj, Integer defaultValue){
return GeneralAlgorithm.nvl(castObjectToInteger(obj),defaultValue);
}
/*
* 将一个Object转换为 Float
*/
public static Float castObjectToFloat(Object obj){
if (obj == null)
return null;
if (obj instanceof Float)
return (Float) obj;
if (obj instanceof Integer)
return ((Integer) obj).floatValue();
if (obj instanceof Long)
return ((Long) obj).floatValue();
if (obj instanceof Float)
return ((Float) obj).floatValue();
if (obj instanceof String)
return parseFloat((String)obj,null);
if (obj instanceof Number)
return ((Number) obj).floatValue();
return parseFloat(StringBaseOpt.objectToString(obj),null);
}
public static Float castObjectToFloat(Object obj, Float defaultValue){
return GeneralAlgorithm.nvl(castObjectToFloat(obj),defaultValue);
}
/*
* 将一个Object转换为 Double
*/
public static Double castObjectToDouble(Object obj){
if (obj == null)
return null;
if (obj instanceof Double)
return (Double) obj;
if (obj instanceof Integer)
return ((Integer) obj).doubleValue();
if (obj instanceof Long)
return ((Long) obj).doubleValue();
if (obj instanceof Float)
return ((Float) obj).doubleValue();
if (obj instanceof String)
return parseDouble((String)obj,null);
if (obj instanceof Number)
return ((Number) obj).doubleValue();
return parseDouble(StringBaseOpt.objectToString(obj),null);
}
public static Double castObjectToDouble(Object obj, Double defaultValue){
return GeneralAlgorithm.nvl(castObjectToDouble(obj),defaultValue);
}
public static BigInteger castObjectToBigInteger(Object obj){
if (obj == null)
return null;
if (obj instanceof BigInteger)
return (BigInteger) obj;
return new BigInteger(StringBaseOpt.objectToString(obj));
}
public static BigInteger castObjectToBigInteger(Object obj, BigInteger defaultValue){
return GeneralAlgorithm.nvl(castObjectToBigInteger(obj),defaultValue);
}
public static BigDecimal castObjectToBigDecimal(Object obj){
if (obj == null)
return null;
if (obj instanceof BigDecimal)
return (BigDecimal) obj;
if (obj instanceof BigInteger)
return new BigDecimal((BigInteger)obj);
if (obj instanceof Double){
return new BigDecimal((Double)obj);
}
return new BigDecimal(StringBaseOpt.objectToString(obj));
}
public static BigDecimal castObjectToBigDecimal(Object obj, BigDecimal defaultValue){
return GeneralAlgorithm.nvl(castObjectToBigDecimal(obj),defaultValue);
}
public int compareTwoLong(Long l1 , Long l2){
return (l1 == null && l2 == null) ? 0:(
l1 == null?-1:(
l2 == null ? 1 :(
Long.compare(l1,l2)
)
)
);
}
public int compareTwoInteger(Integer i1 , Integer i2){
return (i1 == null && i2 == null) ? 0:(
i1 == null?-1:(
i2 == null ? 1 :(
Integer.compare(i1,i2)
)
)
);
}
public int compareTwoDouble(Double d1 , Double d2){
return (d1 == null && d2 == null) ? 0:(
d1 == null?-1:(
d2 == null ? 1 :(
Double.compare(d1,d2)
)
)
);
}
public int compareTwoBigInteger(BigInteger bi1 , BigInteger bi2){
return (bi1 == null && bi2 == null) ? 0:(
bi1 == null?-1:(
bi2 == null ? 1 : bi1.compareTo(bi2)
)
);
}
public int compareTwoBigDecimal(BigDecimal d1 , BigDecimal d2){
return (d1 == null && d2 == null) ? 0:(
d1 == null?-1:(
d2 == null ? 1 : d1.compareTo(d2)
)
);
}
}