![JAR search and dependency download from the Maven repository](/logo.png)
cn.featherfly.conversion.string.format.NumberFormatConvertor Maven / Gradle / Ivy
package cn.featherfly.conversion.string.format;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.util.List;
import cn.featherfly.common.lang.LangUtils;
import cn.featherfly.common.lang.LogUtils;
import cn.featherfly.common.lang.NumberUtils;
import cn.featherfly.common.lang.StringUtils;
import cn.featherfly.conversion.ConversionException;
import cn.featherfly.conversion.string.basic.NumberConvertor;
/**
*
* 数字格式化转换器
*
* @param 转换对象
* @author 钟冀
*/
public class NumberFormatConvertor extends FormatConvertor{
/**
* @param convertor convertor
*/
public NumberFormatConvertor(NumberConvertor convertor) {
super(convertor);
}
/**
* {@inheritDoc}
*/
@Override
protected String formatToString(T value, FormatType genericType) {
if (genericType != null && value != null && StringUtils.isNotBlank(genericType.getFormat())) {
DecimalFormat df = new DecimalFormat(genericType.getFormat());
return df.format(value);
}
return null;
}
/**
* {@inheritDoc}
*/
@Override
protected T formatToObject(String value, FormatType genericType) {
if (genericType != null && value != null && LangUtils.isNotEmpty(genericType.getFormats())) {
List formats = genericType.getFormats();
for (String format : formats) {
DecimalFormat df = new DecimalFormat(format);
try {
return (T) NumberUtils.value(df.parse(value), getSourceType());
} catch (ParseException e) {
LogUtils.debug(e, logger);
}
}
throw new ConversionException("#convert_failed_with_type", new Object[]{
value, formats, getSourceType().getName()});
}
return null;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy