Skip to content

Commit decf379

Browse files
committed
Add test for mixedNumberWordToLong method
Add Billion to mixedNumberWordToLong
1 parent 06016d1 commit decf379

2 files changed

Lines changed: 21 additions & 1 deletion

File tree

  • extractor/src

extractor/src/main/java/org/schabi/newpipe/extractor/utils/Utils.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,16 @@ public static String removeNonDigitCharacters(String toRemove) {
4343
public static long mixedNumberWordToLong(String numberWord) throws NumberFormatException, ParsingException {
4444
String multiplier = "";
4545
try {
46-
multiplier = Parser.matchGroup("[\\d]+([\\.,][\\d]+)?([KMkm])+", numberWord, 2);
46+
multiplier = Parser.matchGroup("[\\d]+([\\.,][\\d]+)?([KMBkmb])+", numberWord, 2);
4747
} catch(ParsingException ignored) {}
4848
double count = Double.parseDouble(Parser.matchGroup1("([\\d]+([\\.,][\\d]+)?)", numberWord));
4949
switch (multiplier.toUpperCase()) {
5050
case "K":
5151
return (long) (count * 1e3);
5252
case "M":
5353
return (long) (count * 1e6);
54+
case "B":
55+
return (long) (count * 1e9);
5456
default:
5557
return (long) (count);
5658
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.schabi.newpipe.extractor.utils;
2+
3+
import com.grack.nanojson.JsonParserException;
4+
import org.junit.Test;
5+
import org.schabi.newpipe.extractor.exceptions.ParsingException;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class UtilsTest {
10+
@Test
11+
public void testMixedNumberWordToLong() throws JsonParserException, ParsingException {
12+
assertEquals(10, Utils.mixedNumberWordToLong("10"));
13+
assertEquals(10.5e3, Utils.mixedNumberWordToLong("10.5K"), 0.0);
14+
assertEquals(10.5e6, Utils.mixedNumberWordToLong("10.5M"), 0.0);
15+
assertEquals(10.5e6, Utils.mixedNumberWordToLong("10,5M"), 0.0);
16+
assertEquals(1.5e9, Utils.mixedNumberWordToLong("1,5B"), 0.0);
17+
}
18+
}

0 commit comments

Comments
 (0)