Here you can find the source of formatPersent(Object input)
Parameter | Description |
---|---|
input | the object that was formated. |
public static String formatPersent(Object input)
//package com.java2s; // %InstallDIR%\features\org.talend.rcp.branding.%PRODUCTNAME%\%PRODUCTNAME%license.txt import java.math.BigDecimal; import java.text.DecimalFormat; import java.util.Locale; public class Main { public static final int PERCENT = 0; public static final int INT_NUMBER = 1; public static final int DOUBLE_NUMBER = 2; /**/* w ww. jav a 2 s . c om*/ * DOC Zqin Comment method "formatPersent". * * @param input the object that was formated. * @return the persent form of this valid input object */ public static String formatPersent(Object input) { if (checkInput(input)) { Double db = new Double(input.toString()); DecimalFormat format = (DecimalFormat) DecimalFormat.getPercentInstance(Locale.ENGLISH); format.applyPattern("0.00%"); //$NON-NLS-1$ return format.format(db); } return null; } /** * DOC Zqin Comment method "checkInput". * * @param input the object that was formated. * @return true if the input is valid, else false; */ private static boolean checkInput(Object input) { if (input == null || "".equals(input)) { //$NON-NLS-1$ return false; } else { Double db = new Double(input.toString()); if (db.equals(Double.NaN)) { return false; } } return true; } /** * DOC Zqin Comment method "format". * * @param input the object that was formated. * @param style the style of formated, it should be 0, 1,2,99999. * @return the formated object. */ public static Object format(Object input, int style) { try { if (checkInput(input)) { DecimalFormat format = null; // MOD qiongli 2011-6-1 bug 21589,if input is out of the decimal precision,replace it with threshold. BigDecimal zero = new BigDecimal(0); BigDecimal temp = new BigDecimal(input.toString()); BigDecimal min = new BigDecimal(10E-5); BigDecimal max = new BigDecimal(9999 * 10E-5); boolean isUseScientific = false; switch (style) { case PERCENT: if (temp.compareTo(min) == -1 && temp.compareTo(zero) == 1) { isUseScientific = true; } else if (temp.compareTo(max) == 1 && temp.compareTo(new BigDecimal(1)) == -1) { input = max.toString(); } format = (DecimalFormat) DecimalFormat.getPercentInstance(Locale.ENGLISH); format.applyPattern("0.00%"); //$NON-NLS-1$ break; case INT_NUMBER: min = new BigDecimal(10E-3); if (temp.compareTo(min) == -1 && temp.compareTo(zero) == 1) { isUseScientific = true; } format = (DecimalFormat) DecimalFormat.getNumberInstance(Locale.ENGLISH); format.applyPattern("0"); //$NON-NLS-1$ break; case DOUBLE_NUMBER: min = new BigDecimal(10E-3); if (temp.compareTo(min) == -1 && temp.compareTo(zero) == 1) { isUseScientific = true; } format = (DecimalFormat) DecimalFormat.getNumberInstance(Locale.ENGLISH); format.applyPattern("0.00"); //$NON-NLS-1$ break; default: format = (DecimalFormat) DecimalFormat.getInstance(Locale.getDefault()); return format.parse(input.toString()); } if (isUseScientific) { format.applyPattern("0.###E0%"); //$NON-NLS-1$ } return format.format(new Double(input.toString())); } else { return input; } } catch (Exception e) { return input; } } }