Java Array Sum sumDigits(final long value, final int... digits)

Here you can find the source of sumDigits(final long value, final int... digits)

Description

Sums all the digits for the given value

License

Apache License

Parameter

Parameter Description
value The source for the sum
digits The list of digits to sum

Return

The sum of all the digits requested

Declaration

public static int sumDigits(final long value, final int... digits) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    /**/*from w  w w.  ja  v  a  2 s.c  om*/
     * Sums all the digits for the given value
     * @param value The source for the sum
     * @param digits The list of digits to sum
     * @return The sum of all the digits requested
     */
    public static int sumDigits(final long value, final int... digits) {
        int result = 0;

        for (final int i : digits) {
            result += decimalDigit(value, i);
        }

        return result;
    }

    /**
     * Return a particular digit value from a given number
     * @param value The source to retrieve the digit
     * @param i The place to retreive
     * @return The digit value
     */
    public static int decimalDigit(final long value, final int i) {
        int result = (int) (Math.abs(value) % Math.pow(10, i));
        if (i > 1) {
            result /= Math.pow(10, i - 1);
        }
        return result;
    }
}

Related

  1. sumaTotal(int vector[])
  2. sumByte(byte[] array)
  3. sumByteArrayToLong(byte[] byteArray)
  4. sumCoefficients(float[] array)
  5. sumdiff(double[] x)
  6. sumDoubleArray(double[] double_array)
  7. sumDoubles(Double[] ds)
  8. sumEachFigureFactor(int[] numbers)
  9. sumElems(boolean[] inputRow)