small changes and support for printing doubles in String class
[IRC.git] / Robust / src / ClassLibrary / String.java
index 147a5b961895bd902894153b342d9fa5b1cbd0ce..7076ebe691a5c0890d84f24661525b036d2987c8 100644 (file)
@@ -288,8 +288,59 @@ public class String {
     return new String(chararray);
   }
 
-  public static String valueOf(double x) {
-    return valueOf((long)x);
+  public static String valueOf(double val) {
+    int i = 0, j = 0, k = 0;
+    long nodecimal = 0;
+    double decimal = 1.0d, valueA = 0.0d;
+    StringBuffer output = new StringBuffer();
+
+    for(i = 0; decimal != nodecimal; i++) {
+      long basePower = 1;
+      for(int x=0; x<i; x++) {
+        basePower*= 10;
+      }
+      nodecimal = (long) (val*basePower);
+      decimal = val*basePower;
+    } //i = place counted from right that decimal point appears
+
+    valueA = nodecimal; //valueA = val with no decimal point (val*10^i)
+
+    for(j = 0; decimal >= 0; j++) {
+      long basePower = 1;
+      for(int x=0; x<j; x++) {
+        basePower*= 10;
+      }
+      nodecimal = (long) (valueA - basePower);
+      decimal = (double) nodecimal;
+    } //j-1 = number of digits
+
+    i--;
+    j--;
+    decimal = 0;
+
+    for(k = j; k > 0; k--) {
+      if(k == i) //if a decimal point was previously found
+      {      //insert it where its meant to be
+        output.append((char)46);
+      }
+      long basePower = 1;
+      for(int x=0; x<(k-1); x++) {
+        basePower*= 10;
+      }
+      nodecimal = ((long) (valueA - decimal) / basePower);
+      decimal += nodecimal*basePower;
+      output.append((char)(48 + nodecimal));
+    }
+
+    return output.toString();
+  }
+
+  public static long basePower(int x, int y) {
+    long t = 1;
+    for(int i=0; i<y; i++) {
+      t *= x;
+    }
+    return t;
   }
 
   public static String valueOf(long x) {