Aligning decimal output in Java

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 :)
 
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.
 
Well there isnt anything useful for doing this in the Interger wrapper class
 
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
 

Members online

No members online now.

Latest profile posts

Also Hi EP and people. I found this place again while looking through a oooollllllldddd backup. I have filled over 10TB and was looking at my collection of antiques. Any bids on the 500Mhz Win 95 fix?
Any of the SP crew still out there?
Xie wrote on Electronic Punk's profile.
Impressed you have kept this alive this long EP! So many sites have come and gone. :(

Just did some crude math and I apparently joined almost 18yrs ago, how is that possible???
hello peeps... is been some time since i last came here.
Electronic Punk wrote on Sazar's profile.
Rest in peace my friend, been trying to find you and finally did in the worst way imaginable.

Forum statistics

Threads
62,015
Messages
673,494
Members
5,621
Latest member
naeemsafi
Back