HiveBrain v1.2.0
Get Started
← Back to all entries
debugjavaMinor

Java number formatting in accountant style

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
formattingnumberjavastyleaccountant

Problem

I have written a string number formatter. I would like to know if this can be improved and any suggestions with how it's done.

The purpose of this function is to convert a double that has been converted to a string, using String.valueOf to a accounting style formatted number.

/**
     * this function formats a number string to show commas and an arbitrary amount
     * of decimal places
     * @param string the number String
     * @param decimal the decimal precision
     * @return 
     */
private String formatNumberString(String string, int decimal) {

    char[] num = string.toCharArray();
    char[] reverse = reverse(num);
    int cnt = 0;
    List chars = new ArrayList();
    boolean fd = false;
    int fCnt = 0;

    if (contains(reverse, '.')) {
        for (char a : reverse) {
            if (!fd) {
                chars.add(a);
            } else {
                if (fCnt == 3) {
                    chars.add(',');
                    fCnt = 0;
                }
                chars.add(a);

                fCnt++;
            }

            if (a == '.') {
                fd = true;
            }

        }
    } else {
        for (char a : reverse) {
            if (fCnt == 3) {
                chars.add(',');
                fCnt = 0;
            }
            chars.add(a);
            fCnt++;
        }

    }
    char[] fin = new char[chars.size()];
    int cntr = 0;
    for (Character ch : chars) {
        fin[cntr++] = ch;
    }

    String finall = String.valueOf(reverse(fin));
    if (decimal == 0) {
        int dot = finall.indexOf(".");
        finall = finall.substring(0, dot);
    }

    if (finall.equals("0") || finall.equals("0.0") || finall.equals("0.00")) {
        finall = " - ";
    }

    return finall;
}


My contains function checks to see the array contains a decimal

```
/**
* this function checks to see if a decimal is present in char array
* @param ch
* @param c
* @return
*/
public bool

Solution

If you come back in a few months to that code you will likely have a problem at figuring out what your code is doing. This is due to the abbreviations you used to name your things like

int cnt = 0;

boolean fd = false;
int fCnt = 0;


for e.g you just won't know what fd should represent.

Naming things is very improtant in the programming field on the other side it is pretty hard as well. Try to give your things meaningful names stating its purpose. By reading the name later you will grasp easier what your code is about.

char[] fin = new char[chars.size()];
int cntr = 0;
for (Character ch : chars) {
    fin[cntr++] = ch;
}


It would be better to just use the toArray() method specified by the List.

For reversing the array you could use ArrayUtils.reverse() see also https://stackoverflow.com/a/2138004/2655508 which lives in Commons.Lang

You contains() and reverse() method are publicbut you aren't doing any parameter validation. If the passed in char[] ch would be null you would get an NullPointerException.

Code Snippets

int cnt = 0;

boolean fd = false;
int fCnt = 0;
char[] fin = new char[chars.size()];
int cntr = 0;
for (Character ch : chars) {
    fin[cntr++] = ch;
}

Context

StackExchange Code Review Q#136275, answer score: 3

Revisions (0)

No revisions yet.