| | |
| | | package org.yeshi.utils;
|
| | |
|
| | | import java.text.DecimalFormat;
|
| | | import java.util.regex.Pattern;
|
| | |
|
| | | public class NumberUtil {
|
| | |
|
| | | /**
|
| | | * 保留2位小数
|
| | | * |
| | | * @param price
|
| | | * @return
|
| | | */
|
| | | public static String get2PointNumber(double price) {
|
| | | DecimalFormat decimalFormat = new DecimalFormat("##0.00");//
|
| | | String p = decimalFormat.format(price);//
|
| | | return p;
|
| | | }
|
| | |
|
| | | /**
|
| | | * 保留1位小数
|
| | | * |
| | | * @param price
|
| | | * @return
|
| | | */
|
| | | public static String get1PointNumber(float price) {
|
| | | DecimalFormat decimalFormat = new DecimalFormat("##0.0");
|
| | | String p = decimalFormat.format(price);
|
| | | return p;
|
| | | }
|
| | |
|
| | | /**
|
| | | * 保留1位小数
|
| | | * |
| | | * @param price
|
| | | * @return
|
| | | */
|
| | | public static String get1PointNumber(double price) {
|
| | | DecimalFormat decimalFormat = new DecimalFormat("##0.0");// 构�?方法的字符格式这里如果小数不�?�?会以0补足.
|
| | | String p = decimalFormat.format(price);// format 返回的是字符�?
|
| | | return p;
|
| | | }
|
| | |
|
| | | private static String getNumberWithZero(int number) {
|
| | | if (number < 10)
|
| | | return "0" + number;
|
| | | else
|
| | | return number + "";
|
| | | }
|
| | |
|
| | | /**
|
| | | * 将秒转为时间字符串
|
| | | * |
| | | * @param second
|
| | | * @return
|
| | | */
|
| | | public static String convertSecondToString(int second) {
|
| | | if (second < 60) {// 小于1分钟
|
| | | return "00:" + getNumberWithZero(second);
|
| | | } else if (second < 60 * 60) {// 小于1小时
|
| | | int minite = second / 60;
|
| | | return getNumberWithZero(minite) + ":" + getNumberWithZero(second % 60);
|
| | | } else {
|
| | | int hour = second / (60 * 60);
|
| | | int minite = (second % (60 * 60)) / 60;
|
| | | int se = (second % (60 * 60)) % 60;
|
| | | return getNumberWithZero(hour) + ":" + getNumberWithZero(minite) + ":" + getNumberWithZero(se);
|
| | | }
|
| | | }
|
| | |
|
| | | /**
|
| | | * 判断字符串中是否包含数字
|
| | | * |
| | | * @param str
|
| | | * @return
|
| | | */
|
| | | public static boolean isNumeric(String str) {
|
| | |
|
| | | if (str == null || "".equals(str.trim())) {
|
| | | return false;
|
| | | }
|
| | |
|
| | | Pattern pattern = Pattern.compile("[0-9]*");
|
| | | return pattern.matcher(str).matches();
|
| | | }
|
| | |
|
| | | /**
|
| | | * 去除小数点后面 多余的0
|
| | | * |
| | | * @param s
|
| | | * @return
|
| | | */
|
| | | public static String subZeroAndDot(String s) {
|
| | | if (s.indexOf(".") > 0) {
|
| | | s = s.replaceAll("0+?$", "");// 去掉多余0
|
| | | s = s.replaceAll("[.]$", "");// 如最后一位是.则去0
|
| | | }
|
| | | return s;
|
| | | }
|
| | | }
|
| | | package org.yeshi.utils; |
| | | |
| | | import java.text.DecimalFormat; |
| | | import java.util.regex.Pattern; |
| | | |
| | | public class NumberUtil { |
| | | |
| | | /** |
| | | * 保留2位小数 |
| | | * |
| | | * @param price |
| | | * @return |
| | | */ |
| | | public static String get2PointNumber(double price) { |
| | | DecimalFormat decimalFormat = new DecimalFormat("##0.00");// |
| | | String p = decimalFormat.format(price);// |
| | | return p; |
| | | } |
| | | |
| | | /** |
| | | * 保留1位小数 |
| | | * |
| | | * @param price |
| | | * @return |
| | | */ |
| | | public static String get1PointNumber(float price) { |
| | | DecimalFormat decimalFormat = new DecimalFormat("##0.0"); |
| | | String p = decimalFormat.format(price); |
| | | return p; |
| | | } |
| | | |
| | | /** |
| | | * 保留1位小数 |
| | | * |
| | | * @param price |
| | | * @return |
| | | */ |
| | | public static String get1PointNumber(double price) { |
| | | DecimalFormat decimalFormat = new DecimalFormat("##0.0");// 构�?方法的字符格式这里如果小数不�?�?会以0补足. |
| | | String p = decimalFormat.format(price);// format 返回的是字符�? |
| | | return p; |
| | | } |
| | | |
| | | private static String getNumberWithZero(int number) { |
| | | if (number < 10) |
| | | return "0" + number; |
| | | else |
| | | return number + ""; |
| | | } |
| | | |
| | | /** |
| | | * 将秒转为时间字符串 |
| | | * |
| | | * @param second |
| | | * @return |
| | | */ |
| | | public static String convertSecondToString(int second) { |
| | | if (second < 60) {// 小于1分钟 |
| | | return "00:" + getNumberWithZero(second); |
| | | } else if (second < 60 * 60) {// 小于1小时 |
| | | int minite = second / 60; |
| | | return getNumberWithZero(minite) + ":" + getNumberWithZero(second % 60); |
| | | } else { |
| | | int hour = second / (60 * 60); |
| | | int minite = (second % (60 * 60)) / 60; |
| | | int se = (second % (60 * 60)) % 60; |
| | | return getNumberWithZero(hour) + ":" + getNumberWithZero(minite) + ":" + getNumberWithZero(se); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 判断字符串中是否包含数字 |
| | | * |
| | | * @param str |
| | | * @return |
| | | */ |
| | | public static boolean isNumeric(String str) { |
| | | |
| | | if (str == null || "".equals(str.trim())) { |
| | | return false; |
| | | } |
| | | |
| | | Pattern pattern = Pattern.compile("[0-9]*"); |
| | | return pattern.matcher(str).matches(); |
| | | } |
| | | |
| | | /** |
| | | * 去除小数点后面 多余的0 |
| | | * |
| | | * @param s |
| | | * @return |
| | | */ |
| | | public static String subZeroAndDot(String s) { |
| | | if (s.indexOf(".") > 0) { |
| | | s = s.replaceAll("0+?$", "");// 去掉多余0 |
| | | s = s.replaceAll("[.]$", "");// 如最后一位是.则去0 |
| | | } |
| | | return s; |
| | | } |
| | | } |