|
|
@@ -1,203 +0,0 @@
|
|
|
-package commons.utility;
|
|
|
-
|
|
|
-import java.math.BigDecimal;
|
|
|
-import java.text.DecimalFormat;
|
|
|
-import java.text.DecimalFormatSymbols;
|
|
|
-
|
|
|
-
|
|
|
-public class NumberUtility {
|
|
|
-
|
|
|
- public static boolean isNumeric(String str)
|
|
|
- {
|
|
|
- return str.matches("-?\\d+(\\.\\d+)?");
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- public static int arrotonda(double valore)
|
|
|
- {
|
|
|
- //valore = Math.round(valore);
|
|
|
- //int ritorno = new Double(valore).intValue();
|
|
|
- return arrotonda(valore,2);
|
|
|
- }
|
|
|
- public static int arrotonda(double valore,int precision)
|
|
|
- {
|
|
|
- BigDecimal value = new BigDecimal(valore);
|
|
|
- if(valore > (double)0) {
|
|
|
- value = value.setScale(2,BigDecimal.ROUND_HALF_UP);
|
|
|
- }
|
|
|
- else {
|
|
|
- value = value.setScale(2,BigDecimal.ROUND_HALF_DOWN);
|
|
|
- }
|
|
|
- return new Double(Math.round(value.doubleValue())).intValue();
|
|
|
- }
|
|
|
-
|
|
|
- public static Double arrotondaDouble(double valore,int precision)
|
|
|
- {
|
|
|
- BigDecimal value = new BigDecimal(valore);
|
|
|
- if(valore > (double)0)
|
|
|
- {
|
|
|
- value = value.setScale(precision,BigDecimal.ROUND_HALF_UP);
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- value = value.setScale(precision,BigDecimal.ROUND_HALF_DOWN);
|
|
|
- }
|
|
|
- return new Double(value.doubleValue());
|
|
|
- }
|
|
|
-
|
|
|
- public static double calcolaPercentuale(double importo, double percentuale)
|
|
|
- {
|
|
|
- return importo * percentuale / 100.0;
|
|
|
- }
|
|
|
-
|
|
|
- public static double dividi(double dividendo, double divisore)
|
|
|
- {
|
|
|
- if (divisore != 0)
|
|
|
- {
|
|
|
- return dividendo / divisore;
|
|
|
- }
|
|
|
- return 0;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * Tentativi falliti
|
|
|
- * public static double truncateNumber(double value, int precision)
|
|
|
- {
|
|
|
- double x = 0;
|
|
|
-
|
|
|
- try
|
|
|
- {
|
|
|
- BigDecimal b1 = new BigDecimal(value);
|
|
|
- return b1.setScale(precision, BigDecimal.ROUND_DOWN).doubleValue();
|
|
|
- }
|
|
|
- catch (Exception e) {
|
|
|
- System.out.println("Modifica truncate:");
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- return x;
|
|
|
- }
|
|
|
-
|
|
|
- public static double truncateNumber(double value,int precision)
|
|
|
- {
|
|
|
- double mult = Math.pow(10.0,precision);
|
|
|
- double x = value > 0 ? Math.floor(value * mult) / mult : Math.ceil(value * mult) / mult;
|
|
|
-
|
|
|
- return new Double(x).doubleValue();
|
|
|
- }
|
|
|
-
|
|
|
- public static double truncateNumber(double value, int precision)
|
|
|
- {
|
|
|
- double x = 0;
|
|
|
-
|
|
|
- try
|
|
|
- {
|
|
|
- int parteIntera = (int) (value * Math.pow(10.0,precision));
|
|
|
- return parteIntera / Math.pow(10.0, precision);
|
|
|
- }
|
|
|
- catch (Exception e) {
|
|
|
- System.out.println("Modifica truncate:");
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- return x;
|
|
|
- }
|
|
|
-
|
|
|
- public static double truncateNumber(double value, int precision)
|
|
|
- {
|
|
|
- double x = arrotondaDouble(value, precision + 3);
|
|
|
- String pattern = "0.0";
|
|
|
-
|
|
|
- for(int i = 0 ; i < precision; i++)
|
|
|
- {
|
|
|
- pattern += "#";
|
|
|
- }
|
|
|
- DecimalFormatSymbols dfs = new DecimalFormatSymbols();
|
|
|
- dfs.setDecimalSeparator('.');
|
|
|
-
|
|
|
- String format = new DecimalFormat(pattern,dfs).format(value);
|
|
|
- if(format.substring(format.indexOf(".") +1 ,format.length()).length() > precision)
|
|
|
- {
|
|
|
- format = format.substring(0,format.length() - 1);
|
|
|
- }
|
|
|
- return Double.parseDouble(format);
|
|
|
- }
|
|
|
- */
|
|
|
- /***
|
|
|
- * Da usare solo in liquidazione
|
|
|
- */
|
|
|
-// public static double truncateNumber(double value, int precision)
|
|
|
-// {
|
|
|
-// String valore = Integer.toString((int)value) + ".";
|
|
|
-// value -= (int)value;
|
|
|
-// for(int i = 0; i < precision; i++)
|
|
|
-// {
|
|
|
-// value *= 10;
|
|
|
-// valore += Integer.toString((int)value);
|
|
|
-// value -= (int)value;
|
|
|
-// }
|
|
|
-//
|
|
|
-// return Double.parseDouble(valore);
|
|
|
-// }
|
|
|
-
|
|
|
- public static double truncateNumber(double value, int precision)
|
|
|
- {
|
|
|
- double valore = arrotondaDouble(value, precision + 3);
|
|
|
- String pattern = "0.0";
|
|
|
-
|
|
|
- for(int i = 0 ; i < precision + 3; i++)
|
|
|
- {
|
|
|
- pattern += "#";
|
|
|
- }
|
|
|
- DecimalFormatSymbols dfs = new DecimalFormatSymbols();
|
|
|
- dfs.setDecimalSeparator('.');
|
|
|
-
|
|
|
- String format = new DecimalFormat(pattern,dfs).format(valore);
|
|
|
- if(format.substring(format.indexOf(".")+1 ,format.length()).length() > precision)
|
|
|
- {
|
|
|
- int lunghezza = format.indexOf(".") + precision + 1;
|
|
|
- format = format.substring(0,Math.min(lunghezza,format.length()));
|
|
|
- }
|
|
|
- return Double.parseDouble(format);
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- public static int valoreAssoluto(int parziale)
|
|
|
- {
|
|
|
- return Math.abs(parziale);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * Arrotonda per eccesso un valore decimale.
|
|
|
- * @param value
|
|
|
- * @return valore intero arrotondato per eccesso
|
|
|
- */
|
|
|
- public static int arrotondaPerEccesso(double value)
|
|
|
- {
|
|
|
- BigDecimal valRet = new BigDecimal(value);
|
|
|
- //approssimo per eccesso
|
|
|
- valRet = valRet.setScale(0, BigDecimal.ROUND_UP);
|
|
|
- return valRet.intValue();
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * Arrotonda per difetto un valore decimale.
|
|
|
- * @param value
|
|
|
- * @return valore intero arrotondato per difetto
|
|
|
- */
|
|
|
- public static int arrotondaPerDifetto(double value)
|
|
|
- {
|
|
|
- BigDecimal valRet = new BigDecimal(value);
|
|
|
- //approssimo per eccesso
|
|
|
- valRet = valRet.setScale(0, BigDecimal.ROUND_DOWN);
|
|
|
- return valRet.intValue();
|
|
|
- }
|
|
|
-
|
|
|
- public static double rapportaAiGiorni(double importo, int giorni)
|
|
|
- {
|
|
|
- return importo * giorni / 365;
|
|
|
- }
|
|
|
-
|
|
|
- public static double rapportaAiMesi(double importo, int mesi)
|
|
|
- {
|
|
|
- return importo * mesi / 12;
|
|
|
- }
|
|
|
-}
|