|
|
@@ -7,43 +7,42 @@ import java.text.DecimalFormat;
|
|
|
import java.util.Locale;
|
|
|
|
|
|
/**
|
|
|
- *
|
|
|
* Created by HooRang on 2017/3/21.
|
|
|
*/
|
|
|
public class NumberFormatUtils {
|
|
|
|
|
|
- private static final int DIVISOR_DEFAULT = 10 ;
|
|
|
+ private static final int DIVISOR_DEFAULT = 10;
|
|
|
|
|
|
- public static float getDivideResult(int dividend){
|
|
|
- if (dividend <= 0){
|
|
|
+ public static float getDivideResult(int dividend) {
|
|
|
+ if (dividend <= 0) {
|
|
|
return 0.0f;
|
|
|
}
|
|
|
|
|
|
BigDecimal bDivisor = new BigDecimal(Double.valueOf(dividend));
|
|
|
BigDecimal bDividend = new BigDecimal(Double.valueOf(DIVISOR_DEFAULT));
|
|
|
- BigDecimal result = bDivisor.divide(bDividend, 1,BigDecimal.ROUND_HALF_DOWN);
|
|
|
+ BigDecimal result = bDivisor.divide(bDividend, 1, BigDecimal.ROUND_HALF_DOWN);
|
|
|
|
|
|
return result.floatValue();
|
|
|
}
|
|
|
|
|
|
- public static float getDivideResult(int dividend, int divisor){
|
|
|
- if (dividend <= 0){
|
|
|
+ public static float getDivideResult(int dividend, int divisor) {
|
|
|
+ if (dividend <= 0) {
|
|
|
return 0.0f;
|
|
|
}
|
|
|
|
|
|
- if (divisor > dividend){
|
|
|
+ if (divisor > dividend) {
|
|
|
return 0.0f;
|
|
|
}
|
|
|
BigDecimal bDivisor = new BigDecimal(Double.valueOf(dividend));
|
|
|
BigDecimal bDividend = new BigDecimal(Double.valueOf(divisor));
|
|
|
- BigDecimal result = bDivisor.divide(bDividend, 1,BigDecimal.ROUND_HALF_DOWN);
|
|
|
+ BigDecimal result = bDivisor.divide(bDividend, 1, BigDecimal.ROUND_HALF_DOWN);
|
|
|
|
|
|
return result.floatValue();
|
|
|
}
|
|
|
|
|
|
- public static double decimal(double val, int newScale){
|
|
|
- BigDecimal b = new BigDecimal(val);
|
|
|
- double f1 = b.setScale(newScale, BigDecimal.ROUND_HALF_UP).doubleValue();
|
|
|
+ public static double decimal(double val, int newScale) {
|
|
|
+ BigDecimal b = new BigDecimal(val);
|
|
|
+ double f1 = b.setScale(newScale, BigDecimal.ROUND_HALF_UP).doubleValue();
|
|
|
return f1;
|
|
|
}
|
|
|
|
|
|
@@ -52,7 +51,7 @@ public class NumberFormatUtils {
|
|
|
}
|
|
|
|
|
|
public static float parseFloat(String numStr, float defaultValue) {
|
|
|
- if(!TextUtils.isEmpty(numStr)){
|
|
|
+ if (!TextUtils.isEmpty(numStr)) {
|
|
|
try {
|
|
|
return Float.parseFloat(numStr);
|
|
|
} catch (NumberFormatException e) {
|
|
|
@@ -67,7 +66,7 @@ public class NumberFormatUtils {
|
|
|
}
|
|
|
|
|
|
public static int parseInteger(String numStr, int defaultValue) {
|
|
|
- if(!TextUtils.isEmpty(numStr)){
|
|
|
+ if (!TextUtils.isEmpty(numStr)) {
|
|
|
try {
|
|
|
return Integer.parseInt(numStr);
|
|
|
} catch (NumberFormatException e) {
|
|
|
@@ -76,12 +75,13 @@ public class NumberFormatUtils {
|
|
|
}
|
|
|
return defaultValue;
|
|
|
}
|
|
|
+
|
|
|
public static long parseLong(String numStr) {
|
|
|
return parseLong(numStr, 0);
|
|
|
}
|
|
|
|
|
|
public static long parseLong(String numStr, int defaultValue) {
|
|
|
- if(!TextUtils.isEmpty(numStr)){
|
|
|
+ if (!TextUtils.isEmpty(numStr)) {
|
|
|
try {
|
|
|
return Long.parseLong(numStr);
|
|
|
} catch (NumberFormatException e) {
|
|
|
@@ -93,6 +93,7 @@ public class NumberFormatUtils {
|
|
|
|
|
|
/**
|
|
|
* 最多保留一位小数
|
|
|
+ *
|
|
|
* @param bonus
|
|
|
* @return
|
|
|
*/
|
|
|
@@ -100,8 +101,10 @@ public class NumberFormatUtils {
|
|
|
DecimalFormat df = new DecimalFormat("#.#");
|
|
|
return df.format(bonus);
|
|
|
}
|
|
|
+
|
|
|
/**
|
|
|
* 最多保留两位小数
|
|
|
+ *
|
|
|
* @param bonus
|
|
|
* @return
|
|
|
*/
|
|
|
@@ -109,8 +112,47 @@ public class NumberFormatUtils {
|
|
|
DecimalFormat df = new DecimalFormat("#.##");
|
|
|
return df.format(bonus);
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 最多保留两位小数(折算:亿、百万、万等)
|
|
|
+ *
|
|
|
+ * @param bonus
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String retainMost2W(String bonus) {
|
|
|
+ return retainMost2W(NumberFormatUtils.parseFloat(bonus));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 最多保留两位小数(折算:亿、百万、万等)
|
|
|
+ *
|
|
|
+ * @param bonus
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String retainMost2W(float bonus) {
|
|
|
+ if (bonus < 1_0000f) {
|
|
|
+ return NumberFormatUtils.retainMost2(bonus);
|
|
|
+ } else if (bonus < 100_0000f) {
|
|
|
+ float number = bonus / 1_0000f;
|
|
|
+ return NumberFormatUtils.retainMost2(number) + "万";
|
|
|
+ } else if (bonus < 10000_0000f) {
|
|
|
+ float number = bonus / 100_0000f;
|
|
|
+ return NumberFormatUtils.retainMost2(number) + "百万";
|
|
|
+ } else if (bonus < 100_0000_0000f) {
|
|
|
+ float number = bonus / 10000_0000f;
|
|
|
+ return NumberFormatUtils.retainMost2(number) + "亿";
|
|
|
+ } else if (bonus < 10000_0000_0000f) {
|
|
|
+ float number = bonus / 100_0000_0000f;
|
|
|
+ return NumberFormatUtils.retainMost2(number) + "百亿";
|
|
|
+ } else {
|
|
|
+ float number = bonus / 10000_0000_0000f;
|
|
|
+ return NumberFormatUtils.retainMost2(number) + "万亿";
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 保留两位小数
|
|
|
+ *
|
|
|
* @param bonus
|
|
|
* @return
|
|
|
*/
|
|
|
@@ -121,7 +163,7 @@ public class NumberFormatUtils {
|
|
|
|
|
|
//获取次数
|
|
|
public static String getText(int no) {
|
|
|
- if(no > 10000)
|
|
|
+ if (no > 10000)
|
|
|
return String.format(Locale.CHINA, "%d万", no / 10000);
|
|
|
return String.valueOf(no);
|
|
|
}
|