Dazu muss man ein wenig selbst Hand anlegen, wie im folgenden Beispiel:
[java]import java.lang.Math;
public class Rounding
{
public static String toString (double d, int place)
{
if (place <= 0)
return ""+(int)(d+((d > 0)? 0.5 : -0.5));
String s = "";
if (d < 0)
{
s += "-";
d = -d;
}
d += 0.5*Math.pow(10,-place);
if (d > 1)
{
int i = (int)d;
s += i;
d -= i;
}
else
s += "0";
if (d > 0)
{
d += 1.0;
String f = ""+(int)(d*Math.pow(10,place));
s += "."+f.substring(1);
}
return s;
}[/code]
Als Beispiel für die Verwendung:
[java]double d = 124.06729562978229;
System.out.println(" intended 124.06729562978229");
System.out.println(" internal "+d);
System.out.println();
System.out.println("rounded to 0 "+Rounding.toString(d,0));
System.out.println("rounded to 1 "+Rounding.toString(d,1));
System.out.println("rounded to 2 "+Rounding.toString(d,2));
System.out.println("rounded to 3 "+Rounding.toString(d,3));
System.out.println("rounded to 4 "+Rounding.toString(d,4));
System.out.println("rounded to 5 "+Rounding.toString(d,5));
System.out.println("rounded to 6 "+Rounding.toString(d,6));
System.out.println("rounded to 7 "+Rounding.toString(d,7));
System.out.println("rounded to 8 "+Rounding.toString(d,8));
System.out.println("rounded to 9 "+Rounding.toString(d,9));[/code]
gibt dann das Ergebnis:
Code:
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
rounded to 0 124
rounded to 1 124.1
rounded to 2 124.07
rounded to 3 124.067
rounded to 4 124.0673
rounded to 5 124.06730
rounded to 6 124.067296
rounded to 7 124.0672956
rounded to 8 124.06729563
rounded to 9 124.067295630
Code:
1 2 3 4 5 6 7 8 9 10
rounded to 0 124
rounded to 1 124.1
rounded to 2 124.07
rounded to 3 124.067
rounded to 4 124.0673
rounded to 5 124.06730
rounded to 6 124.067296
rounded to 7 124.0672956
rounded to 8 124.06729563
rounded to 9 124.067295630
Code:
1 2 3 4 5 6 7 8 9 10
rounded to 0 124
rounded to 1 124.1
rounded to 2 124.07
rounded to 3 124.067
rounded to 4 124.0673
rounded to 5 124.06730
rounded to 6 124.067296
rounded to 7 124.0672956
rounded to 8 124.06729563
rounded to 9 124.067295630
Bei unserer Anwendung macht dies Sinn. Ich habe die Methode eben so umgeschrieben, dass sie auch Zahlen in Exponential-Darstellung schluckt. Dazu wird, wenn ein 'E' gefunden wird einfach dieser Teil abgeschnitten, die Methode dann rekursiv ohne die Exponential-Darstellung aufgerufen und dann der gerundete Wert wieder mit dem Exponenten zurückgegeben. Hier der Code:
public static String toString (double d, int place)
{
String s= new Double(d).toString();
int e = s.indexOf("E");
if(e != -1)
{
String exp = s.substring(e, s.length());
s=s.substring(0,e);
return toString(new Double(s).doubleValue(), place)+exp;
}
else
{
if (place <= 0)
return ""+(int)(d+((d > 0)? 0.5 : -0.5));
s = "";
if (d < 0)
{
s += "-";
d = -d;
}
d += 0.5*Math.pow(10,-place);
if (d > 1)
{
int i = (int)d;
s += i;
d -= i;
}
else
s += "0";
if (d > 0)
{
d += 1.0;
String f = ""+(int)(d*Math.pow(10,place));
s += "."+f.substring(1);
}
return s;
}
}
public static String toString (double d, int place)
{
String s= new Double(d).toString();
int e = s.indexOf("E");
if(e != -1)
{
String exp = s.substring(e, s.length());
s=s.substring(0,e);
return toString(new Double(s).doubleValue(), place)+exp;
}
else
{
if (place <= 0)
return ""+(int)(d+((d > 0)? 0.5 : -0.5));
s = "";
if (d < 0)
{
s += "-";
d = -d;
}
d += 0.5*Math.pow(10,-place);
if (d > 1)
{
int i = (int)d;
s += i;
d -= i;
}
else
s += "0";
if (d > 0)
{
d += 1.0;
String f = ""+(int)(d*Math.pow(10,place));
s += "."+f.substring(1);
}
return s;
}
}
public static String toString (double d, int place)
{
String s= new Double(d).toString();
int e = s.indexOf("E");
if(e != -1)
{
String exp = s.substring(e, s.length());
s=s.substring(0,e);
return toString(new Double(s).doubleValue(), place)+exp;
}
else
{
if (place <= 0)
return ""+(int)(d+((d > 0)? 0.5 : -0.5));
s = "";
if (d < 0)
{
s += "-";
d = -d;
}
d += 0.5*Math.pow(10,-place);
if (d > 1)
{
int i = (int)d;
s += i;
d -= i;
}
else
s += "0";
if (d > 0)
{
d += 1.0;
String f = ""+(int)(d*Math.pow(10,place));
s += "."+f.substring(1);
}
return s;
}
}
Gut, wenns klappt Ihr solltet aber noch darauf achten, dass es keinen Überlauf gibt - bei mir ist aber der 7 Stelle die Zahl total "f#cked up". Das hängt wohl mit der Tatsache zusammen, dass diese Zahl nicht durch 'nen echten Bruch darstellbar ist.
[java]
public String round(double number, int precision)
{
return ("" + new BigDecimal(number).setScale(precision, BigDecimal.ROUND_HALF_UP));
}[/code]
Na gut, der ist unfair, war ja schließlich deine Idee.
O'Dog
_________________ Ich war Atheist, bis ich erkannte, dass ich Gott bin.
Nächstes Thema anzeigen Vorheriges Thema anzeigen
Sie können keine Beiträge in dieses Forum schreiben. Sie können auf Beiträge in diesem Forum nicht antworten. Sie können Ihre Beiträge in diesem Forum nicht bearbeiten. Sie können Ihre Beiträge in diesem Forum nicht löschen. Sie können an Umfragen in diesem Forum nicht mitmachen.
c++.de ist Teilnehmer des Partnerprogramms von Amazon Europe S.à.r.l. und Partner des Werbeprogramms, das zur Bereitstellung eines Mediums
für Websites konzipiert wurde, mittels dessen durch die Platzierung von Werbeanzeigen und Links zu amazon.de
Werbekostenerstattung verdient werden kann.
Die Vervielfältigung der auf den Seiten www.c-plusplus.de, www.c-plusplus.info, www.c-sar.de, www.c-plusplus.net und www.baeckmann.de
enthaltenen Informationen ohne eine schriftliche Genehmigung des Seitenbetreibers ist untersagt
(vgl. §4 Urheberrechtsgesetz). Die Nutzung und Änderung der vorgestellten Strukturen und Verfahren in
privaten und kommerziellen Softwareanwendungen ist ausdrücklich erlaubt, soweit keine Rechte Dritter verletzt werden.
Der Seitenbetreiber übernimmt keine Gewähr für die Funktion einzelner Beiträge oder Programmfragmente, insbesondere
übernimmt er keine Haftung für eventuelle aus dem Gebrauch entstehenden Folgeschäden.