package com.apag.p2plus.p2core;
/**
* Package: P2Plus.P2Core
* Class: Convert.java
* ---------------------------------------------------------------
* Copyright AP-AG, 1999-2004
* ---------------------------------------------------------------
* State: Design
* 30.04.1999 TB generated
* 15.06.1999 TB toDouble() erlaubt auch ',' statt '.'
* 16.08.1999 TB roundMoney(), roundQuantity()
* toDouble irgnoriert führende und folgende SPACEs
* 09.11.1999 TB stringToArray()
* 18.02.2000 TB round() schafft auch sehr große/kleine (mit E) Zahlen
* 06.04.2000 TB round() schafft auch negative Zahlen
* 07.04.2000 TB round() Fehler bei riesengroßen Zahlen behoben
* 16.11.2000 AV round() neu implementiert
* 07.12.2000 TB toFloat()
* 13.12.2000 TB toInt()
* 18.12.2000 AV toShort(),
* toXXX(null) liefert 0 zurück
* 08.01.2001 TB roundMoney() kann eine Währung mitgegeben
* werden und benutzt die Rundung von Hauswährung, wenn
* nichts mitgegeben wird
* 23.01.2001 AV stringToArray2(): Berücksichtigt Leerstrings
* beim Zerlegen des Strings in Teilstrings
* 28.03.2001 AV toLong()
* 08.06.2001 TB toBoolean()
* 16.08.2001 TB alle toXXX(): "" -> 0
* 17.08.2001 TB Bugfix in stringToArray2()
* 29.08.2001 NH charTabToArray Tabs werden durch Leerzeichen ersetzt
* 10.10.2001 AV main()-Methode erweitert, Bugs in round() gefixt
* 04.07.2002 TB stringToObject()
* 31.07.2002 TB round2(), roundSchema()
* 29.01.2003 TB FIX: AF360213 stringToHashSet()
* 06.02.2003 TB FIX: AF360213 round(): Bei fraction=0 wird der Originalwert
* zurückgegeben
* 09.04.2003 TB binaryToHexString(), hexStringToBinary(),
* stringToObject(): Binary-Unterstützung
* 21.03.2005 rkl roundUp() rundet immer auf
* 15.02.2007 HSI stringToArray(String, String, int)
* 15.01.2008 HSI FEATURE: #176619 arrayToString(), arrayToSQLString()
* 12.02.2008 HSI setToString(), setToSQLString()
* 26.06.2008 ISE MapToString(), stringToTreeMap()
* ---------------------------------------------------------------
*/
import java.math.*;
import java.util.*;
// import com.ms.wfc.ui.*;
import com.apag.p2plus.p2core.p2exceptions.*;
import com.apag.p2plus.p2masterdata.*;
import com.apag.p2plus.p2objects.*;
/**
* Diese Klasse beinhaltet Methoden zum Konvertieren und Manipulieren
* verschiedener Datentypen.
*/
public class Convert {
/** main-Methode zu Testzwecken.
* @param args Kommandozeilenargumente
* @throws P2Exception bei einem Fehler
*/
public static void main(String[] args) throws P2Exception {
double res1;
float res2;
int res3;
short res4;
Tool.waitReturn();
res1 = round(12345.6789, 0.001);
if(res1 == 12345.679) {
System.out.println("12345.6789 0.001 -> 12345.679 OK");
} else {
System.out.println("Error: round(12345.6789, 0.001) liefert " + res1 + " anstatt 12345.679.");
}
res1 = roundUp(12344.351, 10.00);
if(res1 == 12350) {
System.out.println("12344.351 10.0 ROUND_UP -> 12350.0 OK");
} else {
System.out.println("Error: roundUp(12344.351, 10.0) liefert " + res1 + " anstatt 12350.0");
}
res1 = round(12345.6789, 0.01);
if(res1 == 12345.68) {
System.out.println("12345.6789 0.01 -> 12345.68 OK");
} else {
System.out.println("Error: round(12345.6789, 0.01) liefert " + res1 + " anstatt 12345.68.");
}
res1 = round(12345.6789, 0.1);
if(res1 == 12345.7) {
System.out.println("12345.6789 0.1 -> 12345.7 OK");
} else {
System.out.println("Error: round(12345.6789, 0.1) liefert " + res1 + " anstatt 12345.7.");
}
res1 = round(12345.6789, 1);
if(res1 == 12346) {
System.out.println("12345.6789 1 -> 12346 OK");
} else {
System.out.println("Error: round(12345.6789, 1) liefert " + res1 + " anstatt 12346.");
}
res1 = round(12345.6789, 10);
if(res1 == 12350) {
System.out.println("12345.6789 10 -> 12350 OK");
} else {
System.out.println("Error: round(12345.6789, 10) liefert " + res1 + " anstatt 12350.");
}
res1 = round(12345.6789, 100);
if(res1 == 12300) {
System.out.println("12345.6789 100 -> 12300 OK");
} else {
System.out.println("Error: round(12345.6789, 100) liefert " + res1 + " anstatt 12300.");
}
res1 = round(12345.6789, 1000);
if(res1 == 12000) {
System.out.println("12345.6789 1000 -> 12000 OK");
} else {
System.out.println("Error: round(12345.6789, 1000) liefert " + res1 + " anstatt 12000.");
}
res1 = round(2157.40, 5.0);
if(res1 == 2155) {
System.out.println(" 2157.40 5.0 -> 2155 OK");
} else {
System.out.println("Error: round(2157.40, 5.0) liefert " + res1 + " anstatt 2155.");
}
res1 = round(2158.40, 3.0);
if(res1 == 2157) {
System.out.println(" 2158.40 3.0 -> 2157 OK");
} else {
System.out.println("Error: round(2158.40, 3.0) liefert " + res1 + " anstatt 2157.");
}
res1 = round(2158.80, 3.0);
if(res1 == 2160) {
System.out.println(" 2158.80 3.0 -> 2160 OK");
} else {
System.out.println("Error: round(2158.80, 3.0) liefert " + res1 + " anstatt 2160.");
}
res1 = round(970d * 1d / 100d, 0.01);
if(res1 == 9.7) {
System.out.println("970 * 1 / 100 0.01 -> 9.7 OK");
} else {
System.out.println("Error: round(970d * 1d / 100d, 0.01) liefert " + res1 + " anstatt 9.7.");
}
res1 = round(30d, 0.01);
if(res1 == 30) {
System.out.println("30 0.01 -> 30 OK");
} else {
System.out.println("Error: round(30d, 0.01) liefert " + res1 + " anstatt 30.");
}
res1 = round(1.905, 0.01);
if(res1 == 1.91) {
System.out.println("1.905 0.01 -> 1.91 OK");
} else {
System.out.println("Error: round(1.905, 0.01) liefert " + res1 + " anstatt 1.91.");
}
res1 = round(10.795, 0.01);
if(res1 == 10.8) {
System.out.println("10.795 0.01 -> 10.8 OK");
} else {
System.out.println("Error: round(10.795, 0.01) liefert " + res1 + " anstatt 10.8.");
}
res1 = round(86.474970534888, 0.01);
if(res1 == 86.47) {
System.out.println("86.474970534888 0.01 -> 86.47 OK");
} else {
System.out.println("Error: round(86.474970534888, 0.01) liefert " + res1 + " anstatt 86.47.");
}
res1 = round(86.474999999999, 0.01);
if(res1 == 86.47) {
System.out.println("86.474999999999 0.01 -> 86.47 OK");
} else {
System.out.println("Error: round(86.474999999999, 0.01) liefert " + res1 + " anstatt 86.47.");
}
res1 = round(86.4755, 0.001);
if(res1 == 86.476) {
System.out.println("86.4755 0.001 -> 86.476 OK");
} else {
System.out.println("Error: round(86.4755, 0.001) liefert " + res1 + " anstatt 86.476.");
}
res1 = round(0.000123456789, 0.000001);
if(res1 == 0.000123) {
System.out.println("0.000123456789 0.000001 -> 0.000123 OK");
} else {
System.out.println("Error: round(0.000123456789, 0.000001) liefert " + res1 + " anstatt 0.000123.");
}
res1 = round(0.12, 0.05);
if(res1 == 0.10) {
System.out.println("0.12 0.05 -> 0.10 OK");
} else {
System.out.println("Error: round(0.12, 0.05) liefert " + res1 + " anstatt 0.10.");
}
res1 = round(0.56, 0.05);
if(res1 == 0.55) {
System.out.println("0.56 0.05 -> 0.55 OK");
} else {
System.out.println("Error: round(0.56, 0.05) liefert " + res1 + " anstatt 0.55.");
}
res1 = round(123, 50);
if(res1 == 100) {
System.out.println("123 50 -> 100 OK");
} else {
System.out.println("Error: round(123, 50) liefert " + res1 + " anstatt 100.");
}
res1 = round(123, 20);
if(res1 == 120) {
System.out.println("123 20 -> 120 OK");
} else {
System.out.println("Error: round(123, 20) liefert " + res1 + " anstatt 120.");
}
System.out.println("round2():");
res1 = round2(123.456, 2);
if(res1 == 123.46) {
System.out.println("123.456 2 -> 123.46 OK");
} else {
System.out.println("Error: round2(123.456, 2) liefert " + res1 + " anstatt 123.46");
}
System.out.println("roundSchema():");
res1 = roundSchema("artikel", "netto", 123.456);
if(res1 == 123.46) {
System.out.println("123.456 2 -> 123.46 OK");
} else {
System.out.println("Error: roundSchema(\"artikel\", \"netto\", 123.456) liefert " + res1 + " anstatt 123.46");
}
System.out.println("toBoolean():");
if(Convert.toBoolean("true")) {
System.out.println("Convert.toBoolean(\"true\") -> true OK");
} else {
System.out.println("Error: Convert.toBoolean(\"true\") liefert false anstatt true.");
}
if(!Convert.toBoolean("false")) {
System.out.println("Convert.toBoolean(\"false\") -> false OK");
} else {
System.out.println("Error: Convert.toBoolean(\"false\") liefert true anstatt false.");
}
/*
String s = "abc\nxyz";
System.out.println(s.replace('\n', ','));
res1 = Convert.toDouble(null);
res2 = Convert.toFloat(null);
res3 = Convert.toInt(null);
res4 = Convert.toShort(null);
*/
/*
System.out.println("\nroundMoney(12345678.123456789, null) -> " + roundMoney(12345678.123456789, null));
System.out.println("roundMoney(12345678.0, null) -> " + roundMoney(12345678.0, null));
System.out.println("roundMoney(1450.0 * 10000.0, null) -> " + roundMoney(1450.0 * 10000.0, null));
*/
System.exit(0);
} // main
/**
* Werte runden.
* Beispiele:
*
* 12345.6789, 0.01 -> 12345.68 * 12345.6789, 10.0 -> 12350.0 * 2157.40 5.0 -> 2155.0 ** * @param value zu rundender Wert * @param fraction Rundungsziel * @return gerundeter Wert */ public static double round(double value, double fraction) { int scale; BigDecimal bigFraction, bigValue; String help; int index, exponent = 0; String exp; if(fraction == 0) { return(value); } // // Workaround für JDK 1.1 // help = Double.toString(value); index = help.indexOf('E'); if(index != -1) { exp = help.substring(index + 1); help = help.substring(0, index); try { exponent = toInt(exp); } catch(P2FormatException fe) { } bigValue = new BigDecimal(help); bigValue = bigValue.movePointRight(exponent); } else { bigValue = new BigDecimal(Double.toString(value)); } help = Double.toString(fraction); index = help.indexOf('E'); if(index != -1) { exp = help.substring(index + 1); help = help.substring(0, index); try { exponent = toInt(exp); } catch(P2FormatException fe) { } bigFraction = new BigDecimal(help); bigFraction = bigFraction.movePointRight(exponent); } else { bigFraction = new BigDecimal(Double.toString(fraction)); } // // Ende Workaround für JDK 1.1 // if(fraction < 1) { // Anzahl der Nachkommastellen ermitteln scale = (int) Math.ceil(Math.log(1 / fraction) / Math.log(10)); } else { scale = 0; } bigValue = bigValue.setScale(scale, BigDecimal.ROUND_HALF_UP); bigValue = bigValue.divide(bigFraction, BigDecimal.ROUND_HALF_UP); bigValue = bigValue.setScale(0, BigDecimal.ROUND_HALF_UP); bigValue = bigValue.multiply(bigFraction); bigValue = bigValue.setScale(scale, BigDecimal.ROUND_HALF_UP); return(bigValue.doubleValue()); } // round /** * Werte runden.
* 12345.6789, 0.01 -> 12345.68 * 12345.6789, 10.0 -> 12350.0 * 2157.40 5.0 -> 2155.0 ** * @param value zu rundender Wert * @param scale Anzahl Nachkommastellen * @return gerundeter Wert */ public static double round2(double value, int scale) { BigDecimal bigFraction, bigValue; String help; int index, exponent = 0; String exp; // // Workaround für JDK 1.1 // help = Double.toString(value); index = help.indexOf('E'); if(index != -1) { exp = help.substring(index + 1); help = help.substring(0, index); try { exponent = toInt(exp); } catch(P2FormatException fe) { } bigValue = new BigDecimal(help); bigValue = bigValue.movePointRight(exponent); } else { bigValue = new BigDecimal(Double.toString(value)); } // // Ende Workaround für JDK 1.1 // bigFraction = new BigDecimal("1"); bigFraction = bigFraction.setScale(scale, BigDecimal.ROUND_HALF_UP); bigFraction = bigFraction.divide(new BigDecimal(Math.pow(10, scale)), BigDecimal.ROUND_HALF_UP); bigValue = bigValue.setScale(scale, BigDecimal.ROUND_HALF_UP); bigValue = bigValue.divide(bigFraction, BigDecimal.ROUND_HALF_UP); bigValue = bigValue.setScale(0, BigDecimal.ROUND_HALF_UP); bigValue = bigValue.multiply(bigFraction); bigValue = bigValue.setScale(scale, BigDecimal.ROUND_HALF_UP); return(bigValue.doubleValue()); } // round2 /** Werte runden.
* 12345.6789, 0.01 -> 12345.68 * 12345.6789, 10.0 -> 12350.0 * 2157.40 5.0 -> 2155.0 ** @return gerundeter Wert * @param tableName Name der Tabelle * @param fieldName Name des Feldes * @param value zu rundender Wert * @throws P2DBException bei einem DB-Fehler */ public static double roundSchema(String tableName, String fieldName, double value) throws P2DBException { return(round2(value, ((SchemaField)SchemaCache.getTable(tableName).getFields().get(fieldName.toUpperCase())).getScale())); } // roundSchema /** Rundet entsprechend der Währung. * @return gerundeter Wert * @param value zu rundender Wert * @param waehrung Währung oder NULL (dann wird die Rundung aus der * Hauswährung verwendet) * @throws P2Exception bei einem Fehler */ public static double roundMoney(double value, String waehrung) throws P2Exception { ObjectManager manager; WaehrungObject waehrungObj; double ret; manager = new ObjectManager(); try { if(waehrung != null) { waehrungObj = WaehrungFactory.search(manager, waehrung, ObjectConstants.READ_LOCK, true); } else { waehrungObj = WaehrungUtil.getBaseCurrency(manager, ObjectConstants.READ_LOCK); } ret = round(value, waehrungObj.getRound()); } finally { manager.release(); } return(ret); } // roundMoney /** * Rundet zur Zeit auf 2 Nachkommastellen. * * @param value zu rundender Wert * @param type Typ des Werts (reserviert) * @return gerundeter Wert */ public static double roundQuantity(double value, String type) { return(round(value, 0.01)); } // roundQuantity /** * Wandelt einen String in ein double. Voraussetzung ist, * daß der String richtig formatiert ist und keine SPACEs * oder Buchstaben enthält. Führende und folgende SPACEs * werden ignoriert. Als Dezimaltrenner darf "," oder "." * verwendet werden. * * @param text zu konvertierender String * @return Zahl * @exception P2FormatException wenn die Konvertierung fehlschlägt */ public static double toDouble(String text) throws P2FormatException { try { if(text != null && text.length()!=0) { // ',' -> '.' text = text.trim().replace(',', '.'); return(Double.valueOf(text).doubleValue()); } else return(0); } catch(NumberFormatException nfe) { throw new P2FormatException("C_CONVERTSTRINGTODOUBLE|" + text); } } // toDouble /** * Wandelt einen String in ein float. Voraussetzung ist, * daß der String richtig formatiert ist und keine SPACEs * oder Buchstaben enthält. Führende und folgende SPACEs * werden ignoriert. Als Dezimaltrenner darf "," oder "." * verwendet werden. * * @param text zu konvertierender String * @return Zahl * @exception P2FormatException wenn die Konvertierung fehlschlägt */ public static float toFloat(String text) throws P2FormatException { try { if(text != null && text.length()!=0) { // ',' -> '.' text = text.trim().replace(',', '.'); return(Float.valueOf(text).floatValue()); } else return(0f); } catch(NumberFormatException nfe) { throw new P2FormatException("C_CONVERTSTRINGTOFLOAT|" + text); } } // toFloat /** * Wandelt einen String in ein int. Voraussetzung ist, * daß der String richtig formatiert ist und keine SPACEs * oder Buchstaben enthält. Führende und folgende SPACEs * werden ignoriert. * * @param text zu konvertierender String * @return Zahl * @exception P2FormatException wenn die Konvertierung fehlschlägt */ public static int toInt(String text) throws P2FormatException { try { if(text != null && text.length()!=0) return(Integer.parseInt(text.trim())); else return(0); } catch(NumberFormatException nfe) { throw new P2FormatException("C_CONVERTSTRINGTOINT|" + text); } } // toInt /** * Wandelt einen String in ein short. Voraussetzung ist, * daß der String richtig formatiert ist und keine SPACEs * oder Buchstaben enthält. Führende und folgende SPACEs * werden ignoriert. * * @param text zu konvertierender String * @return Zahl * @exception P2FormatException wenn die Konvertierung fehlschlägt */ public static short toShort(String text) throws P2FormatException { try { if(text != null && text.length()!=0) return(Short.parseShort(text.trim())); else return(0); } catch(NumberFormatException nfe) { throw new P2FormatException("C_CONVERTSTRINGTOINT|" + text); } } // toShort /** * Wandelt einen String in ein long. Voraussetzung ist, * daß der String richtig formatiert ist und keine SPACEs * oder Buchstaben enthält. Führende und folgende SPACEs * werden ignoriert. * * @param text zu konvertierender String * @return Zahl * @exception P2FormatException wenn die Konvertierung fehlschlägt */ public static long toLong(String text) throws P2FormatException { try { if(text != null && text.length()!=0) return(Long.parseLong(text.trim())); else return(0); } catch(NumberFormatException nfe) { throw new P2FormatException("C_CONVERTSTRINGTOLONG|" + text); } } // toLong /** * Wandelt einen String in ein boolean. Voraussetzung ist, * daß der String richtig formatiert ist und keine SPACEs * oder Buchstaben enthält. Führende und folgende SPACEs * werden ignoriert. * * @param text zu konvertierender String * @return true oder false * @exception P2FormatException wenn die Konvertierung fehlschlägt */ public static boolean toBoolean(String text) throws P2FormatException { if(text != null && text.length()!=0) return(Boolean.valueOf(text.trim()).booleanValue()); else return(false); } // toBoolean /** * Wandelt eine mit SPACEs, TABs und CRLF in einem String * aufgebaute Tabelle um in ein 2-dimensionales String-Array. *
* 12344.350, 0.01 -> 12344.35 * 12344.351, 0.01 -> 12344.36 * 12344.351, 0.10 -> 12344.40 * 12344.351, 1.00 -> 12344.40 * 12344.351, 1.00 -> 12345.00 * 12344.351, 10.00 -> 12350.00 ** @param value zu rundender Wert * @param fraction Rundungsziel * @return gerundeter Wert */ public static double roundUp(double value, double fraction) { int scale; BigDecimal bigFraction, bigValue; if(fraction == 0) { return(value); } bigValue = new BigDecimal(Double.toString(value)); bigFraction = new BigDecimal(Double.toString(fraction)); if(fraction < 1) { // Anzahl der Nachkommastellen ermitteln scale = (int) Math.ceil(Math.log(1 / fraction) / Math.log(10)); } else { scale = 0; } bigValue = bigValue.setScale(scale,BigDecimal.ROUND_UP); bigValue = bigValue.divide(bigFraction, BigDecimal.ROUND_UP); bigValue = bigValue.setScale(0, BigDecimal.ROUND_UP); bigValue = bigValue.multiply(bigFraction); bigValue = bigValue.setScale(scale, BigDecimal.ROUND_UP); return(bigValue.doubleValue()); } // roundUp /** * FIX: F420102 #171162 * Werte abrunden.
* 12344.350, 0.01 -> 12344.35 * 12344.359, 0.01 -> 12344.35 * 12344.351, 0.10 -> 12344.30 * 12344.351, 10.00 -> 12340.00 ** @param value zu rundender Wert * @param fraction Rundungsziel * @return gerundeter Wert */ public static double roundDown(double value, double fraction) { int scale; BigDecimal bigFraction, bigValue; if(fraction == 0) { return(value); } bigValue = new BigDecimal(Double.toString(value)); bigFraction = new BigDecimal(Double.toString(fraction)); if(fraction < 1) { // Anzahl der Nachkommastellen ermitteln scale = (int) Math.ceil(Math.log(1 / fraction) / Math.log(10)); } else { scale = 0; } bigValue = bigValue.setScale(scale,BigDecimal.ROUND_DOWN); bigValue = bigValue.divide(bigFraction, BigDecimal.ROUND_DOWN); bigValue = bigValue.setScale(0, BigDecimal.ROUND_DOWN); bigValue = bigValue.multiply(bigFraction); bigValue = bigValue.setScale(scale, BigDecimal.ROUND_DOWN); return(bigValue.doubleValue()); } // roundDown /** * Zerlegt einen String in Teilstrings anhand eines Separators. * Leerstring werden dabei überlesen. Die Teilstrings werden in einem Array * abgelegt. *
maxTokens
angegeben ist.
* Bsp.: *
* | s: | *"1,2,3,4,5" | *
* | separator: | *"," | *
* | maxTokens: | *2 | *
* | Ergebnis: | *{ "1,2", "3,4", "5" } | *
maxTokens
< 1
*/
public static String[] stringToArray(String s,
String separator,
int maxTokens)
throws P2ParameterException
{
String[] array;
StringTokenizer stTok;
StringBuffer sb;
int count, loop, i, j;
if(maxTokens < 1) {
throw new P2ParameterException("A_PARAMERR|maxTokens|stringToArray()|" +
maxTokens);
}
stTok = new StringTokenizer(s, separator);
count = stTok.countTokens();
loop = count / maxTokens;
if(count % maxTokens != 0) {
loop++;
}
array = new String[loop];
for(i = 0; i < loop; i++) {
sb = new StringBuffer();
for (j = 1; j <= maxTokens && stTok.hasMoreTokens(); j++) {
sb.append(stTok.nextToken()).append(separator);
}
// letzten Separator entfernen
sb.deleteCharAt(sb.length() - 1);
array[i] = sb.toString();
}
return array;
} // stringToArray
/**
* Konvertiert ein Array in einen String anhand eines Separators und einer
* optionalen Zeichenkette.
*
* @param array Array
* @param separator Separator
* @param wrapper optionale Zeichenkette, die um jedes einzelne Element des
* Arrays gesetzt wird
* @return String
* Bsp.: *
* | array: | *{ "A", "ABC", "XY" } | *
* | separator: | *"," | *
* | wrapper: | *"'" | *
* | Ergebnis: | *'A','ABC','XY' | *
Bsp.: *
* | array: | *{ "A", "ABC", "XY" } | *
* | Ergebnis: | *'A','ABC','XY' | *