package com.qxueyou.scc.base.util;
|
|
import java.math.BigDecimal;
|
import java.math.BigInteger;
|
import java.math.MathContext;
|
import java.text.DecimalFormat;
|
import java.util.Random;
|
import java.util.regex.Pattern;
|
|
import org.apache.commons.lang3.StringUtils;
|
import org.springframework.web.servlet.ModelAndView;
|
|
import com.qxueyou.scc.exercise.model.ExerciseItemAnswerU;
|
|
public class CommonUtils {
|
/** һλСÊý */
|
public static final String PARTEN_ONE = "#.#";
|
/** ¶þλСÊý */
|
public static final String PARTEN_TWO = "#.##";
|
|
//µØÇò°ë¾¶
|
private static final double EARTH_RADIUS = 6378.137;
|
|
/**
|
* ´ð°¸ÏÔʾͼÐÎÑÕÉ« ×Ô¶¨Òå
|
* @return
|
*/
|
public static String[] getColors(){
|
String[] colors = new String[]{"#4572a7","#aa4643","#89a54e","#80699b","#3d96ae","#a56f8f","#9f7961","#6f83a5"};
|
|
return colors;
|
}
|
|
/**
|
* ¿Î³Ì±íÏÔʾͼÐÎÑÕÉ« ×Ô¶¨Òå
|
* @return
|
*/
|
public static String[] getRGBColors(){
|
String[] colors = new String[]{"21,199,165","130,191,173","155,238,222","178,235,202","210,210,174","205,186,117",
|
"220,205,166","233,174,160","240,94,104","169,169,169"};
|
|
return colors;
|
}
|
|
/**
|
* »»ËãΪ2λСÊý
|
* @return
|
*/
|
public static String formatDecimalFormat(Object obj, String parten){
|
DecimalFormat decimal = new DecimalFormat(parten);
|
return decimal.format(obj);
|
}
|
|
/**
|
* ɾ³ý×Ö·û×îºóµÄ·ûºÅ
|
* @return
|
*/
|
public static String removeStrLastComma(String string){
|
String str = string;
|
if(StringUtils.isBlank(str)){
|
return str;
|
}
|
String lastStr = str.substring(str.length()-1);
|
|
if(lastStr.indexOf(',') > -1){
|
str = str.substring(0, str.length()-1);
|
}
|
|
return str;
|
}
|
|
/**
|
* ת»»´ð°¸Öµ 0,1,2
|
* @param correct
|
* @return
|
*/
|
public static byte parseInteractCorrectValue(String correct){
|
byte value;
|
|
if("true".equals(correct)){
|
value=ExerciseItemAnswerU.CORRECT_RIGHT;//ÕýÈ·
|
}else if("false".equals(correct)){
|
value=ExerciseItemAnswerU.CORRECT_ERROR;//´íÎó
|
}else{
|
value=7;//δ֪
|
}
|
|
return value;
|
}
|
|
/**
|
* // ½øÐмӷ¨ÔËËã
|
* @param d1
|
* @param d2
|
* @return
|
*/
|
public static double add(double d1, double d2){
|
BigDecimal b1 = new BigDecimal(String.valueOf(d1));
|
BigDecimal b2 = new BigDecimal(String.valueOf(d2));
|
|
return b1.add(b2).doubleValue();
|
}
|
|
/**
|
* // ½øÐмõ·¨ÔËËã
|
* @param d1
|
* @param d2
|
* @return
|
*/
|
public static double subtract(double d1, double d2){
|
BigDecimal b1 = new BigDecimal(String.valueOf(d1));
|
BigDecimal b2 = new BigDecimal(String.valueOf(d2));
|
|
return b1.subtract(b2).doubleValue();
|
}
|
|
/**
|
* // ½øÐг˷¨ÔËËã
|
* @param d1
|
* @param d2
|
* @return
|
*/
|
public static double multiply(double d1, double d2){
|
BigDecimal b1 = new BigDecimal(String.valueOf(d1));
|
BigDecimal b2 = new BigDecimal(String.valueOf(d2));
|
|
return b1.multiply(b2).doubleValue();
|
}
|
|
/**
|
* // ½øÐгý·¨ÔËËã ËÄÉáÎåÈë
|
* @param d1
|
* @param d2
|
* @return
|
*/
|
public static double divide(double d1,double d2,int len) {// ½øÐгý·¨ÔËËã
|
BigDecimal b1 = new BigDecimal(String.valueOf(d1));
|
BigDecimal b2 = new BigDecimal(String.valueOf(d2));
|
|
return b1.divide(b2,len,BigDecimal.ROUND_HALF_UP).doubleValue();
|
}
|
|
/**
|
* // ½øÐгý·¨ÔËËã ËÄÉáÎåÈë
|
* @param d1
|
* @param d2
|
* @return
|
*/
|
public static double divide(String d1,String d2,int len) {// ½øÐгý·¨ÔËËã
|
BigDecimal b1 = new BigDecimal(d1);
|
BigDecimal b2 = new BigDecimal(d2);
|
|
return b1.divide(b2,len,BigDecimal.ROUND_HALF_UP).doubleValue();
|
}
|
|
|
/**
|
* // ½øÐгý·¨ÔËËã ËÄÉáÎåÈë
|
* @param d1
|
* @param d2
|
* @return
|
*/
|
public static BigDecimal divide(BigDecimal b1,BigDecimal b2,int len) {// ½øÐгý·¨ÔËËã
|
|
return b1.divide(b2, new MathContext(len));
|
}
|
public CommonUtils() {
|
// TODO Auto-generated constructor stub
|
}
|
/*
|
*
|
* °´Ö¸¶¨³¤¶È½ØÈ¡Ô´×Ö·û´®£¬ÈôÔ´×Ö·û´®²»´óÓÚÖ¸¶¨³¤¶È£¬ÔòÔÑù·µ»Ø
|
*/
|
public static String cutString(String res, Integer length, String suffix) {
|
if (StringUtils.isEmpty(res)) {
|
return "";
|
}
|
if (res.length() <= length) {
|
return res;
|
}
|
return res.substring(0, length) + (suffix == null ? "" : suffix);
|
}
|
|
/**
|
* Éú³ÉÁ÷Ë®ºÅ £º module+ʱ¼ä´Á + 5Î»Ëæ»úÊý£¬³¤¶È20λ
|
*/
|
public static String generateNo(String module){
|
|
Random rand = new Random();
|
String random = String.valueOf(rand.nextInt(100000));
|
int index = 0;
|
while( random.length() < 5 && index < 5 ){
|
index++;
|
random = "0".concat(random);
|
}
|
|
return module + System.currentTimeMillis() + random ;
|
}
|
|
/**
|
* ¼ÆË㵥λÊý×Ö
|
*
|
* @param number
|
* @return
|
*/
|
public static String getNumberByUnit(BigInteger number){
|
|
if(number == null){
|
return "0";
|
}
|
|
if(number.compareTo(new BigInteger("10000")) < 0){// СÓÚ1Íò
|
return String.valueOf(number);
|
}
|
|
if(number.compareTo(new BigInteger("10000")) >= 0
|
&& number.compareTo(new BigInteger("100000000")) < 0){// ´óÓÚ1Íò СÓÚÒ»ÒÚ Ç°½ø1λΪ1.2Íò
|
|
return Math.ceil(number.doubleValue()/1000)/10 + "Íò";
|
|
}
|
|
if(number.compareTo(new BigInteger("100000000")) >= 0){// ´óÓÚÒ»ÒÚ Ç°½ø1λΪ1.2ÒÚ
|
|
return Math.ceil(number.doubleValue()/1000000)/10 + "ÒÚ";
|
}
|
|
return "0";
|
}
|
|
/**
|
* ¸ñʽ»¯Îı¾
|
* @param str
|
* @return
|
*/
|
public static String formatText(String str){
|
String text = str;
|
if (StringUtils.isNotBlank(str)) {
|
text = text.replace("\n", "<br/>");
|
}
|
return text;
|
}
|
|
//
|
private static double rad(double d){
|
return d * Math.PI / 180.0;
|
}
|
|
//»ñÈ¡Á½¸ö×ø±ê¾àÀë
|
public static double GetDistance(double long1, double lat1, double long2, double lat2) {
|
double a, b, d, sa2, sb2;
|
double latA = rad(lat1);
|
double latB = rad(lat2);
|
a = latA - latB;
|
b = rad(long1 - long2);
|
|
sa2 = Math.sin(a / 2.0);
|
sb2 = Math.sin(b / 2.0);
|
d = 2 * EARTH_RADIUS
|
* Math.asin(Math.sqrt(sa2 * sa2 + Math.cos(latA)
|
* Math.cos(latB) * sb2 * sb2));
|
return d;
|
}
|
|
/**
|
* ÒøÐп¨ÑéÖ¤
|
*
|
* @param cardNo
|
* @return
|
*/
|
public static boolean isBankCard(String cardNo){
|
return Pattern.matches("^\\d{8,27}$", cardNo);
|
}
|
|
/**
|
* ftl-·µ»Ø´íÎóÐÅÏ¢Ò³Ãæ
|
* @param mv
|
* @param msg
|
* @return
|
*/
|
public static ModelAndView resultDeletePage(ModelAndView mv,String msg){
|
mv = new ModelAndView("/deletePage");
|
mv.addObject("content", msg);
|
return mv;
|
}
|
|
/** µÃµ½classId*/
|
public static String getClassId(String classId){
|
return StringUtils.isEmpty(classId)?(StringUtils.isEmpty(ClientUtils.getClassId())?StringUtils.EMPTY:ClientUtils.getClassId()):classId;
|
}
|
}
|