首页 > 代码库 > [JAVA]各种杂七杂八的东西......
[JAVA]各种杂七杂八的东西......
BigInteger / BigDecimal / string 一些常用的函数:
加 add
减 substract
乘 multiply
除 divid
取余 mod / remainder (reminder可用于BigDecimal)
次幂 pow(int)
比较 compareTo / equals
绝对值 abs
相反数 negate
强转为int / double : intValue / doubleValue
a除b的整数部分: divideToIntegralValue (BigDecimal)
判断是否某string开头(是否0开头) startsWith("0");
去掉string前面长度为1的串 substring(1);
数转string string s=a.toString();(会有科学记术法)
toPlainString());
a除b: a.divideAndRemainder(b)[0]
a除b的余数 : a.divideAndRemainder(b)[1]
m=l.getBytes(); // 把 l 的ASCII 存进 m 数组 (其中 static byte[] m; string l;)
去除前导 后导 零
BigDecimal a=a.stripTrailingZeros().toPlainString();
关于这个东西嘛...听说杭电的服务器比较老
所以00.0000这种东西不会去除多余的零 所以要特判.
四舍五入保留2位小数
BigDecimal a=a.setScale(2, BigDecimal.ROUND_HALF_UP);
定义数组
int []a=new int[105];
定义全局变量 要加 static
自定义函数
如 最常用的gcd:
public static BigInteger gcd(BigInteger a, BigInteger b){ return b.compareTo(BigInteger.ZERO)==0? a:gcd(b, a.mod(b));}
快速乘
static BigInteger Pow(BigInteger a, BigInteger b) // a^b{ BigInteger ans=BigInteger.ONE; while(b.compareTo(BigInteger.ZERO)!=0) { if(b.mod(BigInteger.valueOf(2)).compareTo(BigInteger.ZERO)!=0) ans=ans.multiply(a); b=b.divide(BigInteger.valueOf(2)); a=a.multiply(a); } return ans;
}
[JAVA]各种杂七杂八的东西......
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。