package com.topsun.posclient.common.ui.utils;
import java.math.BigDecimal;
import java.util.regex.Pattern;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.widgets.Control;
public class FormatUtils {
public static boolean checkIpFormat(Control control, String value,
String errorMessage) {
String reg = "([1-9]|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}";
if (value.matches(reg)) {
return true;
} else {
MessageDialog.openError(control.getShell(), "错误", errorMessage);
return false;
}
}
/**
* 判断0-1之间的数值,保留小数点2位
*
* @param control
* @param value
* @param errorMessage
* @return
*/
public static boolean checkNeatnessFormat(Control control, String value,
String errorMessage) {
// BigDecimal neatness = BigDecimal.valueOf(Long.valueOf(value));
String reg = "0|1|1.0{1,3}|0.\\d{1,3}";
if (value.matches(reg)) {
return true;
} else {
MessageDialog.openError(control.getShell(), "错误", errorMessage);
return false;
}
}
/**
* 判断大于0 的正整数
*
* @param control
* @param value
* @param errorMessage
* @return
*/
public static boolean chenckNumFormat(Control control, String value,
String errorMessage) {
if (isInteger(value) || isDouble(value)) {
return true;
} else {
MessageDialog.openError(control.getShell(), "错误", errorMessage);
control.forceFocus();
return false;
}
}
/*
* 判断是否为浮点数,包括double和float
*
* @param str 传入的字符串
*
* @return 是浮点数返回true,否则返回false
*/
public static boolean isDouble(String str) {
Pattern pattern = Pattern.compile("^[-\\+]?[.\\d]*$");
return pattern.matcher(str).matches();
}
/*
* 判断是否为整数
*
* @param str 传入的字符串
*
* @return 是整数返回true,否则返回false
*/
public static boolean isInteger(String str) {
Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
return pattern.matcher(str).matches();
}
public static boolean oldGoldcheckWeightG(Control control, String value,
String errorMessage) {
String reg = "^\\d+(\\.\\d{0,3})?$";
if (value.matches(reg)) {
return true;
} else {
MessageDialog.openError(control.getShell(), "错误", errorMessage);
return false;
}
}
public static boolean checkWeightG(Control control, String value,
String errorMessage) {
String reg = "^\\d+(\\.\\d+)?$";
if (value.matches(reg)) {
return true;
} else {
MessageDialog.openError(control.getShell(), "错误", errorMessage);
return false;
}
}
public static void main(String[] args) {
// String reg = "^\\d+(\\.\\d+)?$";
//
// String[] ss = new String[]{"1","0","0.999","-1","2","-0.9"};
// for (String string : ss) {
// System.out.println(string + " " +string.matches(reg));
// }
// BigDecimal bdtest = new BigDecimal(4545.159);
// System.out.println(FormatUtils.convetC(bdtest));
}
}