Aligning decimal output in Java

Discussion in 'Web Design & Coding' started by seamus_padraig, Dec 10, 2004.

  1. seamus_padraig

    seamus_padraig OSNN One Post Wonder

    Messages:
    2
    How dow you align decimal numbers when outputting them to the console in Java?
     
  2. Mainframeguy

    Mainframeguy Debiant by way of Ubuntu Folding Team

    Messages:
    3,763
    Location:
    London, UK
    dunno about Java especially, but typicaly this will involve casting the variable from a numeric type into a string type, depending on the sophistication of the language and implementation this may be enough, or you may need to apply a mask or somesuch to specify the alignment required or to remove leading/trailing zeros.

    Why not give an example of your need? IE do you want them aligned like this?

    123.45
    __1.023

    (for _ ready space!) or in some other way?

    Welcome to OSNN :)
     
  3. seamus_padraig

    seamus_padraig OSNN One Post Wonder

    Messages:
    2
    Yeah, your example is exactly what I'm trying to do. I want to right-align a series of dollar figures on the decimal point for output. Java has formatting classes for all the other stuff (commas, dollar signs, etc.); just nothing for right-alignment on the decimal.
     
  4. X-Istence

    X-Istence * Political User

    Messages:
    6,498
    Location:
    USA
    Go poke Geffy, he is our resident Java expert.
     
  5. Geffy

    Geffy Moderator Folding Team

    Messages:
    7,805
    Location:
    United Kingdom
    Well there isnt anything useful for doing this in the Interger wrapper class
     
  6. Geffy

    Geffy Moderator Folding Team

    Messages:
    7,805
    Location:
    United Kingdom
    Code:
    /**
     * Number Formatting, either by the length of the numeric string
     * or by the length from the first number to the decimal point.
     *
     * @author Geoffrey Garside <java AT stealth-ninja DOT co DOT uk>
     * @version 1.0, 2004/12/11
     */
    
    public class NumberFormat {
        public static String[] alignByLength(final int [] numbers) {
            // Create Similar array but for strings
            String [] formatted = numbersToStrings(numbers);
            
            int length = maxLength(formatted);
            formatted = appendSpaces(formatted, length);
            
            return formatted;
        }
        
        public static String[] alignByLength(final double [] numbers) {
            // Create Similar array but for strings
            String [] formatted = numbersToStrings(numbers);
            
            int length = maxLength(formatted);
            formatted = appendSpaces(formatted, length);
            
            return formatted;
        }
        
        public static String[] alignByLength(final float [] numbers) {
            // Create Similar array but for strings
            String [] formatted = numbersToStrings(numbers);
            
            int length = maxLength(formatted);
            formatted = appendSpaces(formatted, length);
            
            return formatted;
        }
        
        public static String[] alignByDecimal(final double [] numbers) {
            // Create Similar array but for strings
            String [] formatted = numbersToStrings(numbers);
            
            int length = maxLength(formatted, '.');
            formatted = appendSpaces(formatted, length);
            
            return formatted;
        }
        
        public static String[] alignByDecimal(final float [] numbers) {
            // Create Similar array but for strings
            String [] formatted = numbersToStrings(numbers);
            
            int length = maxLength(formatted, '.');
            formatted = appendSpaces(formatted, length);
            
            return formatted;
        }
        
        private static String[] appendSpaces(String[] strings, final int length) {
            // ok, now we need to add some spaces to the beginning
            // of each number so that they should all align properly
            for (int i = 0; i < strings.length; i++) {
                if (strings[i].length() < length) {
                    int difference = length - strings[i].length();
                    
                    strings[i] = spaces(difference) + strings[i];
                }
            }
            
            return strings;
        }
        
        private static String spaces(final int length) {
            StringBuffer spaces = new StringBuffer();
            for (int i = 0; i < length; i++) {
                spaces.append(" ");
            }
            
            return spaces.toString();
        }
        
        private static int maxLength(final String[] strings, final char find) {
            int length = 0;
            for (int i = 0; i < strings.length; i++) {
                if (strings[i].indexOf(find) > length) {
                    length = strings[i].indexOf(find);
                }
            }
            
            return length;
        }
        
        private static int maxLength(final String[] strings) {
            // lets find the longest number
            int length = 0;
            for (int i = 0; i < strings.length; i++) {
                if (strings[i].length() > length) {
                    length = strings[i].length();
                }
            }
            
            return length;
        }
        
        private static String[] numbersToStrings(final int [] numbers) {
            // Create Similar array but for strings
            String [] strings = new String[numbers.length];
            
            for (int i = 0; i < strings.length; i++) {
                strings[i] = ""+ numbers[i];
            }
            
            return strings;
        }
        
        private static String[] numbersToStrings(final double [] numbers) {
            // Create Similar array but for strings
            String [] strings = new String[numbers.length];
            
            for (int i = 0; i < strings.length; i++) {
                strings[i] = ""+ numbers[i];
            }
            
            return strings;
        }
        
        private static String[] numbersToStrings(final float [] numbers) {
            // Create Similar array but for strings
            String [] strings = new String[numbers.length];
            
            for (int i = 0; i < strings.length; i++) {
                strings[i] = ""+ numbers[i];
            }
            
            return strings;
        }
    }
    something I just whipped up, I havent tested it beyond compilation. I also cant be arsed to comment it properly right now, but I might do that later