1、加法運(yùn)算
public static String add(String v1, String v2) {
?? ??? ??? ?BigDecimal b1 = new BigDecimal(v1);
?? ??? ??? ?BigDecimal b2 = new BigDecimal(v2);
?? ??? ??? ?return b1.add(b2).toString();
?? ??? ?}
2、減法運(yùn)算
public static String sub(String v1, String v2) {
?? ??? ??? ?BigDecimal b1 = new BigDecimal(v1);
?? ??? ??? ?BigDecimal b2 = new BigDecimal(v2);
?? ??? ??? ?return b1.subtract(b2).toString();
?? ??? ?}
3、乘法運(yùn)算
public static String mul(String v1, String v2) {
?? ??? ??? ?BigDecimal b1 = new BigDecimal(v1);
?? ??? ??? ?BigDecimal b2 = new BigDecimal(v2);
?? ??? ??? ?return b1.multiply(b2).toString();
?? ??? ?}
4、除法運(yùn)算。當(dāng)發(fā)生除不盡的情況時(shí),由scale參數(shù)指定精度,以后的數(shù)字四舍五入
public static String div(String v1, String v2, int scale) {
?? ??? ??? ?if (scale < 0) {
?? ??? ??? ??? ?throw new IllegalArgumentException(
?? ??? ??? ??? ?"The scale must be a positive integer or zero");
?? ??? ??? ?}
?? ??? ??? ?BigDecimal b1 = new BigDecimal(v1);
?? ??? ??? ?BigDecimal b2 = new BigDecimal(v2);
?? ??? ??? ?return (b1.divide(b2, scale, 4)).toString();
?? ??? ?}
5、除法運(yùn)算。當(dāng)發(fā)生除不盡的情況時(shí),由scale參數(shù)指定精度,以后的數(shù)字直接舍掉
public static String mydiv(String v1, String v2, int scale) {
?? ??? ??? ?if (scale < 0) {
?? ??? ??? ??? ?throw new IllegalArgumentException(
?? ??? ??? ??? ?"The scale must be a positive integer or zero");
?? ??? ??? ?}
?? ??? ??? ?BigDecimal b1 = new BigDecimal(v1);
?? ??? ??? ?BigDecimal b2 = new BigDecimal(v2);
?? ??? ??? ?return b1.divide(b2).setScale(scale,1).toString();
?? ??? ?}
6、精確的小數(shù)位四舍五入處理
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,4).doubleValue();
?? ??? ?}
7、value1 與 value2作比較:不相等返回true 否則返回false
public static boolean isNotEquals(String value1,String value2){
?? ??? ??? ?try {
?? ??? ??? ??? ?if(new BigDecimal(value1).compareTo(new BigDecimal(value2)) != 0){
?? ??? ??? ??? ??? ?return true;
?? ??? ??? ??? ?}else{
?? ??? ??? ??? ??? ?return false;
?? ??? ??? ??? ?}
?? ??? ??? ?} catch (Exception e) {
?? ??? ??? ??? ?return true;
?? ??? ??? ?}
?? ??? ?}
8、str1 = str2 :true 否則:false
public static boolean eq(String str1,String str2){
?? ??? ??? ?try {
?? ??? ??? ??? ?if(new BigDecimal(str1).compareTo(new BigDecimal(str2)) == 0){
?? ??? ??? ??? ??? ?return true;
?? ??? ??? ??? ?}else{
?? ??? ??? ??? ??? ?return false;
?? ??? ??? ??? ?}
?? ??? ??? ?} catch (Exception e) {
?? ??? ??? ??? ?return false;
?? ??? ??? ?}
?? ??? ?}
9、?str1 != str2 :true 否則:false
public static boolean neq(String str1,String str2){
?? ??? ??? ?try {
?? ??? ??? ??? ?if(new BigDecimal(str1).compareTo(new BigDecimal(str2)) != 0){
?? ??? ??? ??? ??? ?return true;
?? ??? ??? ??? ?}else{
?? ??? ??? ??? ??? ?return false;
?? ??? ??? ??? ?}
?? ??? ??? ?} catch (Exception e) {
?? ??? ??? ??? ?return false;
?? ??? ??? ?}
?? ??? ?}
10、str1 > str2 :true 否則:false
public static boolean gt(String str1,String str2){
?? ??? ??? ?try {
?? ??? ??? ??? ?if(new BigDecimal(str1).compareTo(new BigDecimal(str2)) > 0){
?? ??? ??? ??? ??? ?return true;
?? ??? ??? ??? ?}else{
?? ??? ??? ??? ??? ?return false;
?? ??? ??? ??? ?}
?? ??? ??? ?} catch (Exception e) {
?? ??? ??? ??? ?return false;
?? ??? ??? ?}
?? ??? ?}
11、str1 >= str2 :true 否則:false
public static boolean egt(String str1,String str2){
?? ??? ??? ?try {
?? ??? ??? ??? ?if(new BigDecimal(str1).compareTo(new BigDecimal(str2)) >= 0){
?? ??? ??? ??? ??? ?return true;
?? ??? ??? ??? ?}else{
?? ??? ??? ??? ??? ?return false;
?? ??? ??? ??? ?}
?? ??? ??? ?} catch (Exception e) {
?? ??? ??? ??? ?return false;
?? ??? ??? ?}
?? ??? ?}
12、str1 < str2 :true 否則:false
public static boolean lt(String str1,String str2){
?? ??? ??? ?try {
?? ??? ??? ??? ?if(new BigDecimal(str1).compareTo(new BigDecimal(str2)) < 0){
?? ??? ??? ??? ??? ?return true;
?? ??? ??? ??? ?}else{
?? ??? ??? ??? ??? ?return false;
?? ??? ??? ??? ?}
?? ??? ??? ?} catch (Exception e) {
?? ??? ??? ??? ?return false;
?? ??? ??? ?}
?? ??? ?}
13、?str1 <= str2 :true 否則:false
public static boolean elt(String str1,String str2){
?? ??? ??? ?try {
?? ??? ??? ??? ?if(new BigDecimal(str1).compareTo(new BigDecimal(str2)) <= 0){
?? ??? ??? ??? ??? ?return true;
?? ??? ??? ??? ?}else{
?? ??? ??? ??? ??? ?return false;
?? ??? ??? ??? ?}
?? ??? ??? ?} catch (Exception e) {
?? ??? ??? ??? ?return false;
?? ??? ??? ?}
?? ??? ?}
14、str1 >= str2 并且 str1 < str3 :true 否則:false
public static boolean between(String str1,String str2,String str3){
?? ??? ??? ?try {
?? ??? ??? ??? ?if(new BigDecimal(str1).compareTo(new BigDecimal(str2)) >= 0 &&?
?? ??? ??? ??? ??? ??? ?new BigDecimal(str1).compareTo(new BigDecimal(str3)) < 0){
?? ??? ??? ??? ??? ?return true;
?? ??? ??? ??? ?}else{
?? ??? ??? ??? ??? ?return false;
?? ??? ??? ??? ?}
?? ??? ??? ?} catch (Exception e) {
?? ??? ??? ??? ?return false;
?? ??? ??? ?}
?? ??? ?}