Aligning decimal output in Java

Mainframeguy

Debiant by way of Ubuntu
#2
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
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.
 

Geffy

OSNN Veteran Addict
#6
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 posts

Latest profile posts

Perris Calderon wrote on Electronic Punk's profile.
Ep, glad to see you come back and tidy up...did want to ask a one day favor, I want to enhance my resume , was hoping you could make me administrator for a day, if so, take me right off since I won't be here to do anything, and don't know the slightest about the board, but it would be nice putting "served administrator osnn", if can do, THANKS

Been running around Quora lately, luv it there https://tinyurl.com/ycpxl
Electronic Punk wrote on Perris Calderon's profile.
All good still mate?
Hello, is there anybody in there? Just nod if you can hear me ...
Xie
What a long strange trip it's been. =)

Forum statistics

Threads
61,969
Messages
673,295
Members
89,016
Latest member
dopexoity