/* * StrLib.java - assorted string manipulation routines, * mostly for formatted output of floating point numbers * Copyright 1999-2003 the Shodor Edcuation * Foundation, Inc. */ import java.awt.*; import java.applet.Applet; class StrLib { public static String expNLength(double x,int n) { if(n<12) {n=12;} String number=expNotation(x); String before=""; String after=""; String exp=""; int iflag=0; for(int i=0;i1000.0) { iflag=1; int icount=0; while(my_arg>10.0){ icount++; my_arg/=10.0; } exp=Integer.toString(icount); } my_arg*=1000.0; my_arg+=0.5; int iarg =(int) my_arg; my_arg=(double)iarg; my_arg/=1000.0; number=Double.toString(my_arg); if(iflag==1) { number+="E"+exp; } return sign+number; } public static String round(double num, int sig){ String retval, temp; double mantissa; int exponent; double rounded; temp = new String(""+num); if (temp.indexOf("e") != -1) { exponent = (new Integer(temp.substring(temp.indexOf("e")+1))). intValue(); mantissa = (new Double(temp.substring(0, temp.indexOf("e")-1))). doubleValue(); } else if (temp.indexOf("E") != -1) { exponent = (new Integer(temp.substring(temp.indexOf("E")+1))). intValue(); mantissa = (new Double(temp.substring(0, temp.indexOf("E")-1))). doubleValue(); } else { mantissa = (new Double(temp)).doubleValue(); exponent = 0; } if(Math.abs(mantissa)>10000 || Math.abs(mantissa)<.001 && mantissa != 0){ while(Math.abs(mantissa) < 1){ mantissa *= 10; exponent--; } while(Math.abs(mantissa) >= 10){ mantissa /= 10; exponent++; } } if(mantissa != 0.0){ mantissa = (double)Math.round(mantissa * (double)Math.pow(10, sig)) /(double)Math.pow(10, sig); } retval = new String(""+mantissa); if(exponent != 0) retval += "e" + exponent; return(retval); } public static String stripLeading(String instring) { //Strip any occurences of the given character off of the beginning // of a string int length=instring.length(); if(length==0) return ""; boolean done = false; int pos=0; while(!done){ //Check for existance of character. char firstchar = instring.charAt(pos); if(firstchar==' '||firstchar==','||firstchar=='\t'|| firstchar=='\n') { pos++; if (pos==length) return ""; } else { done=true; } } if(pos==0) { return instring; } else { return instring.substring(pos); } } }