How does this code add a comma before every three digits?

Help me understand how this code works. It essentially adds a comma to the string of numbers. Therefore, if the user enters a number from 1 to 3 digits, it does not change. For a four-digit number, it adds a comma, so

  • 1111 becomes 1.111
  • 11111 becomes 11.111
  • 111111111 becomes 11,111,111

etc. Here is the code:

private String addCommasToNumericString (String digits)
{
    String result = "";
    int len = digits.length();
    int nDigits = 0;

    for (int i = len - 1; i >= 0; i--)                      
    {
        result = digits.charAt(i) + result;                 
        nDigits++;                                          
        if (((nDigits % 3) == 0) && (i > 0))                
        {
            result = "," + result;
        }
    }
    return (result);
}

I will explain that I understand about it

The loop forbasically calculates the length of the number that the user wrote so as not to put a comma before the first number (for example, 1111). And although it is iless than the length of the string, it subtracts 1.

resultreturns a char at a position i, as it counts down, it returns the characters “opposite” from right to left.

nDigits 1 0 .

, , : if ("nDigits % 3) == 0.

, if , :

  • 1% 3 = 1
  • 2% 3 = 2
  • 3% 3 = 0

nDigits 1 - nDigits++ for, , ? , 4 5 , 1 (1,111 - 11,111)?

+5
6

, - .

, , 12345, "5", nDigits 1.

"4" "4", "45", nDigits 2.

"3" , "345", if . ", 345".

"12,345".

, , "5", "1". , , .

, !

+6

. , .

String Manipulation char. , , .

subString . RIGHT To LEFT, , 3- .

private String addCommas (String digits) {

    String result = digits;

    if (digits.length() <= 3) return digits; // If the original value has 3 digits or  less it returns that value

    for (int i = 0; i < (digits.length() – 1) / 3; i++) {

      int commaPos = digits.length() – 3 – (3 * i); // comma position in each cicle

      result = result.substring(0, commaPos) + "," + result.substring(commaPos);
    }
   return result;
}
+2

result , (.. ).

  • char ,

    result = digits.charAt(i) + result;  
    

  • char ,

    result = "," + result;
    

, Java result = "," + result; . StringBuffer StringBuilder.

+1
for (int i = len - 1; i >= 0; i--)

i len - 1, . i > 0 if (((nDigits % 3) == 0) && (i > 0)) - , (, 1111).

0

, , , result 3 .

len , nDigits - , . len-1 ( , ), for 0 ( ). i, result, , . nDigits % 3 0 3- , if , , , 3 , , , 0.

0

@Antonio Ricardo Diegues Silva .

/**
 * Get number in {@link String} with divider after 'n' number of digits
 * @param number number for processing
 * @param n amount after which will be inserted divider
 * @return {@link String} number with dividers
 */
public static <T extends Number> String insertDividerBetweenEveryNDigits(T number, int n, String divider) {
    StringBuilder builder = new StringBuilder().append(number);
    int digitsNumber = builder.length();
    if (digitsNumber > n) { // If the original value has n digits or less it just returns that value
        for (int i = 1; i <= (digitsNumber - 1) / n; i++) {
            int dividerPos = digitsNumber - (n * i); // divider position in each cycle
            builder.insert(dividerPos, divider);
        }
    }
    return builder.toString();
}
0

All Articles