Here you can find the source of sumDigits(final long value, final int... digits)
Parameter | Description |
---|---|
value | The source for the sum |
digits | The list of digits to sum |
public static int sumDigits(final long value, final int... digits)
//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; } }