Skip to content

Commit 6d504e0

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

2 files changed

Lines changed: 23 additions & 2 deletions

File tree

  • extractor/src

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,17 @@ 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) {}
48-
double count = Double.parseDouble(Parser.matchGroup1("([\\d]+([\\.,][\\d]+)?)", numberWord));
48+
double count = Double.parseDouble(Parser.matchGroup1("([\\d]+([\\.,][\\d]+)?)", numberWord)
49+
.replace(",", "."));
4950
switch (multiplier.toUpperCase()) {
5051
case "K":
5152
return (long) (count * 1e3);
5253
case "M":
5354
return (long) (count * 1e6);
55+
case "B":
56+
return (long) (count * 1e9);
5457
default:
5558
return (long) (count);
5659
}
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)