home *** CD-ROM | disk | FTP | other *** search
- /*
- * BigNum.class converts a very large number to a string
- * and then prints it out
- */
- class BigNum {
-
- public static void main (String args[]) {
-
- /*
- * Good thing longs are 64 bits!
- */
- long debt = 3965170506502L;
- long pop = 250410000;
-
- print ("The 1992 national debt was $", debt);
- print ("The 1992 population was ", pop);
- print ("Each person owed $", debt/pop);
- }
-
- /*
- * print method converts a long to a string with commas
- * for easy reading. We need it with these big numbers!
- * String str preceding label
- * long n the long to format and print
- */
- public static void print (String str,long n) {
- System.out.print (str); // print the label
- String buf = String.valueOf (n); // Integer to String
-
- int start, end, ncommas, i;
- int buflen = buf.length ();
-
- /*
- * It's a crazy algorithm, It works from left to right
- */
- ncommas = buflen/3;
- if (ncommas * 3 == buflen) ncommas -= 1;
- start = 0;
- end = buflen-(3*ncommas);
- System.out.print (buf.substring (start, end));
- for (i=0; i<ncommas; i+=1) {
- start = end;
- end = start+3;
- System.out.print (",");
- System.out.print (buf.substring (start, end));
- }
- System.out.println (""); // The final newline
- }
- }