下面列出了org.apache.commons.lang.math.NumberUtils#toDouble ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private String chooseSegment(String value, SpecParam p) {
double val = NumberUtils.toDouble(value);
String result = "其它";
// 保存数值段
for (String segment : p.getSegments().split(",")) {
String[] segs = segment.split("-");
// 获取数值范围
double begin = NumberUtils.toDouble(segs[0]);
double end = Double.MAX_VALUE;
if (segs.length == 2) {
end = NumberUtils.toDouble(segs[1]);
}
// 判断是否在范围内
if (val >= begin && val < end) {
if (segs.length == 1) {
result = segs[0] + p.getUnit() + "以上";
} else if (begin == 0) {
result = segs[1] + p.getUnit() + "以下";
} else {
result = segment + p.getUnit();
}
break;
}
}
return result;
}
public static double getDouble( Object obj ) {
if ( obj instanceof Double ) {
return ( Double ) obj;
}
if ( obj instanceof Number ) {
return ( ( Number ) obj ).doubleValue();
}
if ( obj instanceof String ) {
return NumberUtils.toDouble( ( String ) obj );
}
if ( obj instanceof Date ) {
return ( ( Date ) obj ).getTime();
}
if ( obj instanceof byte[] ) {
return getDouble( ( byte[] ) obj );
}
if ( obj instanceof ByteBuffer ) {
return getDouble( ( ByteBuffer ) obj );
}
return 0;
}
public static double getDouble( Object obj ) {
if ( obj instanceof Double ) {
return ( Double ) obj;
}
if ( obj instanceof Number ) {
return ( ( Number ) obj ).doubleValue();
}
if ( obj instanceof String ) {
return NumberUtils.toDouble( ( String ) obj );
}
if ( obj instanceof Date ) {
return ( ( Date ) obj ).getTime();
}
if ( obj instanceof byte[] ) {
return getDouble( ( byte[] ) obj );
}
if ( obj instanceof ByteBuffer ) {
return getDouble( ( ByteBuffer ) obj );
}
return 0;
}
/**
* 比较double类型
*
* @param instanceAlertConfig 报警配置
* @param currentValue 当前值
* @return
*/
protected boolean isCompareDoubleRight(InstanceAlertConfig instanceAlertConfig, double currentValue) {
double alertValue = NumberUtils.toDouble(instanceAlertConfig.getAlertValue());
int compareType = instanceAlertConfig.getCompareType();
if (compareType == InstanceAlertCompareTypeEnum.LESS_THAN.getValue() && currentValue < alertValue) {
return false;
} else if (compareType == InstanceAlertCompareTypeEnum.MORE_THAN.getValue() && currentValue > alertValue) {
return false;
} else if (compareType == InstanceAlertCompareTypeEnum.EQUAL.getValue() && currentValue == alertValue) {
return false;
} else if (compareType == InstanceAlertCompareTypeEnum.NOT_EQUAL.getValue() && currentValue != alertValue) {
return false;
}
return true;
}
@Override
public List<InstanceAlertValueResult> checkConfig(InstanceAlertConfig instanceAlertConfig, AlertConfigBaseData alertConfigBaseData) {
// 检查内存
Object usedMemoryObject = getValueFromRedisInfo(alertConfigBaseData.getStandardStats(), RedisInfoEnum.used_memory.getValue());
long usedMemory = NumberUtils.toLong(usedMemoryObject.toString());
if (usedMemory < MIN_CHECK_MEMORY) {
return null;
}
// 内存碎片率
Object memFragmentationRatioObject = getValueFromRedisInfo(alertConfigBaseData.getStandardStats(), RedisInfoEnum.mem_fragmentation_ratio.getValue());
if (memFragmentationRatioObject == null) {
return null;
}
// 关系比对
double memFragmentationRatio = NumberUtils.toDouble(memFragmentationRatioObject.toString());
boolean compareRight = isCompareDoubleRight(instanceAlertConfig, memFragmentationRatio);
if (compareRight) {
return null;
}
InstanceInfo instanceInfo = alertConfigBaseData.getInstanceInfo();
InstanceAlertValueResult instanceAlertValueResult = new InstanceAlertValueResult(instanceAlertConfig, instanceInfo, String.valueOf(memFragmentationRatio),
instanceInfo.getAppId(), EMPTY);
instanceAlertValueResult.setOtherInfo(String.format("内存使用为%s MB", String.valueOf(changeByteToMB(usedMemory))));
return Arrays.asList();
}
/**
* 从top的cpuLine解析出us
* @param cpuLine
* @return
*/
public static double getUsCpu(String cpuLine) {
if (cpuLine == null || EMPTY_STRING.equals(cpuLine.trim())) {
return 0;
}
String[] items = cpuLine.split(COMMA);
if (items.length < 1) {
return 0;
}
String usCpuStr = items[0];
return NumberUtils.toDouble(matchCpuLine(usCpuStr));
}
@Override
protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
Locale locale = req.getLocale();
String strNumber = req.getParameter("number");
double number = NumberUtils.toDouble(strNumber, -1);
try {
StringBuilder bodyHtml = new StringBuilder();
bodyHtml.append("<form action=\"te\" method=\"post\">");
bodyHtml.append(getMsg("msg.enter.positive.number", locale));
bodyHtml.append("<br><br>");
bodyHtml.append("10.0 " + getMsg("label.obelus", locale) + " ");
if (0 < number && number < 10) {
bodyHtml.append(
"<input type=\"text\" name=\"number\" size=\"1\" maxlength=\"1\" value=" + strNumber + ">");
} else {
bodyHtml.append("<input type=\"text\" name=\"number\" size=\"1\" maxlength=\"1\">");
}
bodyHtml.append(" = ");
if (0 < number && number < 10) {
bodyHtml.append(String.valueOf(10.0 / number));
}
bodyHtml.append("<br><br>");
bodyHtml.append("<input type=\"submit\" value=\"" + getMsg("label.calculate", locale) + "\">");
bodyHtml.append("<br><br>");
bodyHtml.append(getInfoMsg("msg.note.truncationerror", locale));
bodyHtml.append("</form>");
responseToClient(req, res, getMsg("title.truncationerror.page", locale), bodyHtml.toString());
} catch (Exception e) {
log.error("Exception occurs: ", e);
}
}
@Override
protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
boolean isValid = true;
Locale locale = req.getLocale();
String strNumber = req.getParameter("number");
double number = NumberUtils.toDouble(strNumber, Double.NaN);
try {
if (Double.isNaN(number) || number <= -1 || 1 <= number) {
isValid = false;
}
StringBuilder bodyHtml = new StringBuilder();
bodyHtml.append("<form action=\"lotd\" method=\"post\">");
bodyHtml.append(getMsg("msg.enter.decimal.value", locale));
bodyHtml.append("<br><br>");
if (!Double.isNaN(number) && isValid) {
bodyHtml.append("<input type=\"text\" name=\"number\" size=\"18\" maxlength=\"18\" value=" + strNumber + ">");
} else {
bodyHtml.append("<input type=\"text\" name=\"number\" size=\"18\" maxlength=\"18\">");
}
bodyHtml.append(" + 1 = ");
if (!Double.isNaN(number) && isValid) {
bodyHtml.append(String.valueOf(number + 1));
}
bodyHtml.append("<br><br>");
bodyHtml.append("<input type=\"submit\" value=\"" + getMsg("label.calculate", locale) + "\">");
bodyHtml.append("<br><br>");
bodyHtml.append(getInfoMsg("msg.note.lossoftrailingdigits", locale));
bodyHtml.append("</form>");
responseToClient(req, res, getMsg("title.lossoftrailingdigits.page", locale), bodyHtml.toString());
} catch (Exception e) {
log.error("Exception occurs: ", e);
}
}
/**
* Builds particle settings from a configuration section.
*
* @param location the location for this particle settings.
* @param config the config section for the settings.
* @return a parsed ParticleDisplay.
* @since 1.0.0
*/
@Nonnull
public static ParticleDisplay fromConfig(@Nullable Location location, @Nonnull ConfigurationSection config) {
Objects.requireNonNull(config, "Cannot parse ParticleDisplay from a null config section");
Particle particle = XParticle.getParticle(config.getString("particle"));
if (particle == null) particle = Particle.FLAME;
int count = config.getInt("count");
double extra = config.getDouble("extra");
double offsetx = 0, offsety = 0, offsetz = 0;
String offset = config.getString("offset");
if (offset != null) {
String[] offsets = StringUtils.split(offset, ',');
if (offsets.length > 0) {
offsetx = NumberUtils.toDouble(offsets[0]);
if (offsets.length > 1) {
offsety = NumberUtils.toDouble(offsets[1]);
if (offsets.length > 2) {
offsetz = NumberUtils.toDouble(offsets[2]);
}
}
}
}
double x = 0, y = 0, z = 0;
String rotation = config.getString("rotation");
if (rotation != null) {
String[] rotations = StringUtils.split(rotation, ',');
if (rotations.length > 0) {
x = NumberUtils.toDouble(rotations[0]);
if (rotations.length > 1) {
y = NumberUtils.toDouble(rotations[1]);
if (rotations.length > 2) {
z = NumberUtils.toDouble(rotations[2]);
}
}
}
}
float[] rgbs = null;
String color = config.getString("color");
if (color != null) {
String[] colors = StringUtils.split(rotation, ',');
if (colors.length >= 3) rgbs = new float[]
{NumberUtils.toInt(colors[0]), NumberUtils.toInt(colors[1]), NumberUtils.toInt(colors[2]),
(colors.length > 3 ? NumberUtils.toFloat(colors[0]) : 1.0f)};
}
Vector rotate = new Vector(x, y, z);
ParticleDisplay display = new ParticleDisplay(particle, location, count, offsetx, offsety, offsetz, extra);
display.rotation = rotate;
display.data = rgbs;
return display;
}
/**
* Parses the bindingConfig of an item and returns a HomematicBindingConfig.
*/
public HomematicBindingConfig parse(Item item, String bindingConfig) throws BindingConfigParseException {
bindingConfig = StringUtils.trimToEmpty(bindingConfig);
bindingConfig = StringUtils.removeStart(bindingConfig, "{");
bindingConfig = StringUtils.removeEnd(bindingConfig, "}");
String[] entries = bindingConfig.split("[,]");
HomematicBindingConfigHelper helper = new HomematicBindingConfigHelper();
for (String entry : entries) {
String[] entryParts = StringUtils.trimToEmpty(entry).split("[=]");
if (entryParts.length != 2) {
throw new BindingConfigParseException("Each entry must have a key and a value");
}
String key = StringUtils.trim(entryParts[0]);
// convert entry id to device if necessary
if ("id".equalsIgnoreCase(key)) {
logger.info("Please change the Homematic binding with the attribute 'id' to 'address' in entry: "
+ entry + " -> " + StringUtils.replace(entry, "id=", "address="));
key = "address";
}
String value = StringUtils.trim(entryParts[1]);
value = StringUtils.removeStart(value, "\"");
value = StringUtils.removeEnd(value, "\"");
try {
helper.getClass().getDeclaredField(key).set(helper, value);
} catch (Exception e) {
throw new BindingConfigParseException("Could not set value " + value + " for attribute " + key);
}
}
Converter<?> converter = null;
// if (helper.isValidDatapoint() || helper.isValidVariable()) {
// converter = instantiateConverter(helper.converter);
// }
BindingAction bindingAction = getBindingAction(item, helper.action);
if (helper.isValidDatapoint()) {
return new DatapointConfig(helper.address, helper.channel, helper.parameter, converter, bindingAction,
helper.isForceUpdate(), NumberUtils.toDouble(helper.delay));
} else if (helper.isValidVariable()) {
return new VariableConfig(helper.variable, converter, bindingAction, helper.isForceUpdate(),
NumberUtils.toDouble(helper.delay));
} else if (helper.isValidProgram()) {
if (!acceptsOnOffType(item)) {
throw new BindingConfigParseException(
"Programs can only be attached to items which accepts OnOffType commands, ignoring item "
+ item.getName());
}
return new ProgramConfig(helper.program, bindingAction);
} else if (bindingAction != null) {
return new ActionConfig(bindingAction);
} else {
throw new BindingConfigParseException("Invalid binding: " + bindingConfig);
}
}
/**
* 保留两位
* @param num
* @return
*/
private double remainNumberTwoPoint(double num) {
DecimalFormat df = new DecimalFormat("0.00");
return NumberUtils.toDouble(df.format(num));
}