Java tutorial
//package com.java2s; /** * Copyright (c) 2015-present, Horcrux. * All rights reserved. * * This source code is licensed under the MIT-style license found in the * LICENSE file in the root directory of this source tree. */ import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main { static private Pattern percentageRegExp = Pattern.compile("^(\\-?\\d+(?:\\.\\d+)?)%$"); /** * Converts percentage string into actual based on a relative number * * @param percentage percentage string * @param relative relative number * @param offset offset number * @return actual float based on relative number */ static float fromPercentageToFloat(String percentage, float relative, float offset, float scale) { Matcher matched = percentageRegExp.matcher(percentage); if (matched.matches()) { return Float.valueOf(matched.group(1)) / 100 * relative + offset; } else { return Float.valueOf(percentage) * scale + offset; } } }