Add test for mixedNumberWordToLong method
Add Billion to mixedNumberWordToLong
This commit is contained in:
parent
06016d1ae3
commit
6d504e0883
|
@ -43,14 +43,17 @@ public class Utils {
|
||||||
public static long mixedNumberWordToLong(String numberWord) throws NumberFormatException, ParsingException {
|
public static long mixedNumberWordToLong(String numberWord) throws NumberFormatException, ParsingException {
|
||||||
String multiplier = "";
|
String multiplier = "";
|
||||||
try {
|
try {
|
||||||
multiplier = Parser.matchGroup("[\\d]+([\\.,][\\d]+)?([KMkm])+", numberWord, 2);
|
multiplier = Parser.matchGroup("[\\d]+([\\.,][\\d]+)?([KMBkmb])+", numberWord, 2);
|
||||||
} catch(ParsingException ignored) {}
|
} catch(ParsingException ignored) {}
|
||||||
double count = Double.parseDouble(Parser.matchGroup1("([\\d]+([\\.,][\\d]+)?)", numberWord));
|
double count = Double.parseDouble(Parser.matchGroup1("([\\d]+([\\.,][\\d]+)?)", numberWord)
|
||||||
|
.replace(",", "."));
|
||||||
switch (multiplier.toUpperCase()) {
|
switch (multiplier.toUpperCase()) {
|
||||||
case "K":
|
case "K":
|
||||||
return (long) (count * 1e3);
|
return (long) (count * 1e3);
|
||||||
case "M":
|
case "M":
|
||||||
return (long) (count * 1e6);
|
return (long) (count * 1e6);
|
||||||
|
case "B":
|
||||||
|
return (long) (count * 1e9);
|
||||||
default:
|
default:
|
||||||
return (long) (count);
|
return (long) (count);
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,18 @@
|
||||||
|
package org.schabi.newpipe.extractor.utils;
|
||||||
|
|
||||||
|
import com.grack.nanojson.JsonParserException;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.schabi.newpipe.extractor.exceptions.ParsingException;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
public class UtilsTest {
|
||||||
|
@Test
|
||||||
|
public void testMixedNumberWordToLong() throws JsonParserException, ParsingException {
|
||||||
|
assertEquals(10, Utils.mixedNumberWordToLong("10"));
|
||||||
|
assertEquals(10.5e3, Utils.mixedNumberWordToLong("10.5K"), 0.0);
|
||||||
|
assertEquals(10.5e6, Utils.mixedNumberWordToLong("10.5M"), 0.0);
|
||||||
|
assertEquals(10.5e6, Utils.mixedNumberWordToLong("10,5M"), 0.0);
|
||||||
|
assertEquals(1.5e9, Utils.mixedNumberWordToLong("1,5B"), 0.0);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue