Java number formatting in accountant style

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<Character> 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 boolean contains(char[] ch, char c) {         for (char a : ch) {             if (a == c) {                 return true;             }         }         return false;     } 

My reverse function is simple it reverses the char array

 public char[] reverse(char[] ch) {         char[] temp = new char[ch.length];         int cnt = 0;         for (int i = ch.length - 1; i >= 0; i--) {             temp[cnt++] = ch[i];         }         return temp;      } 

Replay

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<E>.



For reversing the array you could use ArrayUtils.reverse() see also http://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.

Category: java Time: 2016-07-29 Views: 0

Related post

iOS development

Android development

Python development

JAVA development

Development language

PHP development

Ruby development

search

Front-end development

Database

development tools

Open Platform

Javascript development

.NET development

cloud computing

server

Copyright (C) avrocks.com, All Rights Reserved.

processed in 0.144 (s). 12 q(s)