mirror of
https://github.com/shekhargulati/strman-java.git
synced 2026-10-20 06:24:05 +00:00
* Partially resolved #78 (Hacktoberfest initiative) * Removing trailing whitespaces
This commit is contained in:
@@ -1389,6 +1389,29 @@ public abstract class Strman {
|
||||
return resultBuilder.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a string representation of the number passed in where groups of three digits are delimited by comma
|
||||
*
|
||||
* @param number Input number
|
||||
* @return formatted String
|
||||
*/
|
||||
public static String formatNumber(long number) {
|
||||
String stringRepresentation = Long.toString(number);
|
||||
StringBuilder sb = new StringBuilder();
|
||||
int bound = stringRepresentation.length() - 1;
|
||||
String delimiter = ",";
|
||||
int counter = 0;
|
||||
for (int i = bound; i >= 0; i--) {
|
||||
char c = stringRepresentation.charAt(i);
|
||||
if (i != bound && counter % 3 == 0) {
|
||||
sb.append(delimiter);
|
||||
}
|
||||
sb.append(c);
|
||||
counter++;
|
||||
}
|
||||
return sb.reverse().toString();
|
||||
}
|
||||
|
||||
public static String[] chop(String input, int step) {
|
||||
if (input == null || input.length() == 0) {
|
||||
return EMPTY_ARRAY;
|
||||
|
||||
@@ -1187,6 +1187,14 @@ public class StrmanTests {
|
||||
assertThat(chop("whitespace", 0)[0].length(), equalTo(10));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void formatNumber_shouldFormatNumberWithCommaDelimiter() throws Exception {
|
||||
assertThat(formatNumber(1000), equalTo("1,000"));
|
||||
assertThat(formatNumber(100000), equalTo("100,000"));
|
||||
assertThat(formatNumber(10000000), equalTo("10,000,000"));
|
||||
assertThat(formatNumber(100000000), equalTo("100,000,000"));
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void startCase_shouldThrowException() throws Exception {
|
||||
startCase(null);
|
||||
|
||||
Reference in New Issue
Block a user