From a851f94b5b63a86792304e360933e0bf85b1464b Mon Sep 17 00:00:00 2001 From: vee w Date: Sun, 30 Sep 2018 10:42:14 +0700 Subject: [PATCH] v1.1.6 * Fix `toBytes()` on float/double/contain dot number to return false. * Fix zero leading for full number. (change`$num{0}` to just `$num`). * Fix do not display net for convert Thai Baht. * Fix dot number to dot number zero, for example: 1.2 to 1.20 before convert Thai Baht. * Remove zero leading in `convertNumberWithScale()` method. * Add more tests for via-http and phpunit. --- Rundiz/Number/Number.php | 7 +- Rundiz/Number/NumberEng.php | 2 +- Rundiz/Number/NumberThai.php | 16 +- tests/phpunit/ConvertNumberTest.php | 387 +++++++++++++++++ tests/phpunit/ConvertTHBTest.php | 408 ++++++++++++++++++ tests/phpunit/NumberTest.php | 39 +- tests/via-http/_convert-numbers-set-test.php | 31 ++ tests/via-http/_functions.php | 36 ++ tests/via-http/convert-en.php | 18 +- tests/via-http/convert-frombyte.php | 2 +- tests/via-http/convert-number-shorter.php | 7 +- tests/via-http/convert-th.php | 22 +- tests/via-http/convert-thb.php | 28 +- tests/via-http/convert-tobyte.php | 13 +- .../merge-convert-number-languages.php | 26 ++ .../via-http/merge-convert-thb-languages.php | 26 ++ 16 files changed, 980 insertions(+), 88 deletions(-) create mode 100644 tests/phpunit/ConvertNumberTest.php create mode 100644 tests/phpunit/ConvertTHBTest.php create mode 100644 tests/via-http/_convert-numbers-set-test.php create mode 100644 tests/via-http/_functions.php create mode 100644 tests/via-http/merge-convert-number-languages.php create mode 100644 tests/via-http/merge-convert-thb-languages.php diff --git a/Rundiz/Number/Number.php b/Rundiz/Number/Number.php index a14072d..c822c55 100644 --- a/Rundiz/Number/Number.php +++ b/Rundiz/Number/Number.php @@ -3,7 +3,7 @@ * Number class. * * @package Number - * @version 1.1.5 + * @version 1.1.6 * @author Vee W. * @license http://opensource.org/licenses/MIT * @@ -194,6 +194,11 @@ public function toBytes($size) } unset($pattern); + if (!isset($matches[2]) && isset($matches[1]) && strpos($matches[1], '.') !== false) { + // if no size unit and the number is float, double, contain dot. cannot convert. + return false; + } + $size_number = 0; if (isset($matches[1])) { $size_number = $matches[1]; diff --git a/Rundiz/Number/NumberEng.php b/Rundiz/Number/NumberEng.php index 2ff10bb..ec03f3e 100644 --- a/Rundiz/Number/NumberEng.php +++ b/Rundiz/Number/NumberEng.php @@ -99,7 +99,7 @@ public function convertNumber($num) $num = ltrim($num, '+'); } - if ($num{0} == '0') { + if ($num == '0') { $output .= 'zero'; } else { if (strlen($num) > 36) { diff --git a/Rundiz/Number/NumberThai.php b/Rundiz/Number/NumberThai.php index 810a1ec..a1f8134 100644 --- a/Rundiz/Number/NumberThai.php +++ b/Rundiz/Number/NumberThai.php @@ -73,7 +73,7 @@ public function convertBaht($num, $display_net = true) $num = ltrim($num, '+'); } - if ($num{0} == '0') { + if ($num == '0') { $output .= 'ศูนย์'; } else { $output .= $this->convertNumberWithScale($num); @@ -83,16 +83,19 @@ public function convertBaht($num, $display_net = true) if ($dec > 0) { // if there is decimal (.) $dec_str = ''; - + + if (strlen($dec) == 1) { + $dec .= '0'; + } // convert number normally for decimal. $dec_str = $this->convertNumberWithScale($dec); - + if ($dec_str != null) { $output .= $dec_str . 'สตางค์'; } } - if (!isset($dec_str) || (isset($dec_str) && $dec_str == null) && $display_net === true) { + if ($display_net === true && (!isset($dec_str) || (isset($dec_str) && $dec_str == null))) { $output .= 'ถ้วน'; } @@ -143,7 +146,7 @@ public function convertNumber($num) $num = ltrim($num, '+'); } - if ($num{0} == '0') { + if ($num == '0') { $output .= 'ศูนย์'; } else { $output .= $this->convertNumberWithScale($num); @@ -170,11 +173,12 @@ public function convertNumber($num) /** * convert the number to text with scale. (ten, hundred, thousand, ...) in Thai language. * - * @param number $digits number only. no negative or positive sign. no dot. + * @param string $digits number only. no negative or positive sign. no dot. * @return string */ private function convertNumberWithScale($digits) { + $digits = ltrim($digits, '0');// remove zero leading. example: 0212, 00213 $length_digit = strlen($digits); $count = 1; $pos = 0;// หลักเลข 1=หน่วย, 2=สิบ, 3=ร้อย, ... diff --git a/tests/phpunit/ConvertNumberTest.php b/tests/phpunit/ConvertNumberTest.php new file mode 100644 index 0000000..1a22c43 --- /dev/null +++ b/tests/phpunit/ConvertNumberTest.php @@ -0,0 +1,387 @@ + -9210000000, + 'resultEng' => 'negative nine billion, two hundred and ten million', + 'resultThai' => 'ลบเก้าพันสองร้อยสิบล้าน' + ), + array( + 'number' => '-1000000', + 'resultEng' => 'negative one million', + 'resultThai' => 'ลบหนึ่งล้าน' + ), + array( + 'number' => '-100000', + 'resultEng' => 'negative one hundred thousand', + 'resultThai' => 'ลบหนึ่งแสน' + ), + array( + 'number' => '-10000', + 'resultEng' => 'negative ten thousand', + 'resultThai' => 'ลบหนึ่งหมื่น' + ), + array( + 'number' => -12, + 'resultEng' => 'negative twelve', + 'resultThai' => 'ลบสิบสอง' + ), + array( + 'number' => '-0.1234', + 'resultEng' => 'negative zero point one two three four', + 'resultThai' => 'ลบศูนย์จุดหนึ่งพันสองร้อยสามสิบสี่' + ), + array( + 'number' => '+5.45', + 'resultEng' => 'positive five point four five', + 'resultThai' => 'บวกห้าจุดสี่สิบห้า' + ), + array( + 'number' => 0, + 'resultEng' => 'zero', + 'resultThai' => 'ศูนย์' + ), + array( + 'number' => 0.123456789, + 'resultEng' => 'zero point one two three four five six seven eight nine', + 'resultThai' => 'ศูนย์จุดหนึ่งร้อยยี่สิบสามล้านสี่แสนห้าหมื่นหกพันเจ็ดร้อยแปดสิบเก้า' + ), + array( + 'number' => 0.05, + 'resultEng' => 'zero point zero five', + 'resultThai' => 'ศูนย์จุดศูนย์ห้า' + ), + array( + 'number' => 0.2, + 'resultEng' => 'zero point two', + 'resultThai' => 'ศูนย์จุดสอง' + ), + array( + 'number' => '0.20', + 'resultEng' => 'zero point two zero', + 'resultThai' => 'ศูนย์จุดยี่สิบ' + ), + array( + 'number' => 0.23, + 'resultEng' => 'zero point two three', + 'resultThai' => 'ศูนย์จุดยี่สิบสาม' + ), + array( + 'number' => 0.202, + 'resultEng' => 'zero point two zero two', + 'resultThai' => 'ศูนย์จุดสองร้อยสอง' + ), + array( + 'number' => 0.99, + 'resultEng' => 'zero point nine nine', + 'resultThai' => 'ศูนย์จุดเก้าสิบเก้า' + ), + array( + 'number' => 0.2102, + 'resultEng' => 'zero point two one zero two', + 'resultThai' => 'ศูนย์จุดสองพันหนึ่งร้อยสอง' + ), + array( + 'number' => '0212', + 'resultEng' => 'two hundred and twelve', + 'resultThai' => 'สองร้อยสิบสอง' + ), + array( + 'number' => '00213', + 'resultEng' => 'two hundred and thirteen', + 'resultThai' => 'สองร้อยสิบสาม' + ), + array( + 'number' => '1.00', + 'resultEng' => 'one', + 'resultThai' => 'หนึ่ง' + ), + array( + 'number' => 324.5, + 'resultEng' => 'three hundred and twenty-four point five', + 'resultThai' => 'สามร้อยยี่สิบสี่จุดห้า' + ), + array( + 'number' => '324.50', + 'resultEng' => 'three hundred and twenty-four point five zero', + 'resultThai' => 'สามร้อยยี่สิบสี่จุดห้าสิบ' + ), + array( + 'number' => 1, + 'resultEng' => 'one', + 'resultThai' => 'หนึ่ง' + ), + array( + 'number' => 5, + 'resultEng' => 'five', + 'resultThai' => 'ห้า' + ), + array( + 'number' => 10, + 'resultEng' => 'ten', + 'resultThai' => 'สิบ' + ), + array( + 'number' => 11, + 'resultEng' => 'eleven', + 'resultThai' => 'สิบเอ็ด' + ), + array( + 'number' => 12, + 'resultEng' => 'twelve', + 'resultThai' => 'สิบสอง' + ), + array( + 'number' => 13, + 'resultEng' => 'thirteen', + 'resultThai' => 'สิบสาม' + ), + array( + 'number' => 20, + 'resultEng' => 'twenty', + 'resultThai' => 'ยี่สิบ' + ), + array( + 'number' => 21, + 'resultEng' => 'twenty-one', + 'resultThai' => 'ยี่สิบเอ็ด' + ), + array( + 'number' => 30, + 'resultEng' => 'thirty', + 'resultThai' => 'สามสิบ' + ), + array( + 'number' => 33, + 'resultEng' => 'thirty-three', + 'resultThai' => 'สามสิบสาม' + ), + array( + 'number' => 100, + 'resultEng' => 'one hundred', + 'resultThai' => 'หนึ่งร้อย' + ), + array( + 'number' => 101, + 'resultEng' => 'one hundred and one', + 'resultThai' => 'หนึ่งร้อยเอ็ด' + ), + array( + 'number' => 1000, + 'resultEng' => 'one thousand', + 'resultThai' => 'หนึ่งพัน' + ), + array( + 'number' => 1001, + 'resultEng' => 'one thousand and one', + 'resultThai' => 'หนึ่งพันเอ็ด' + ), + array( + 'number' => 1010, + 'resultEng' => 'one thousand and ten', + 'resultThai' => 'หนึ่งพันสิบ' + ), + array( + 'number' => 1011, + 'resultEng' => 'one thousand and eleven', + 'resultThai' => 'หนึ่งพันสิบเอ็ด' + ), + array( + 'number' => 1012, + 'resultEng' => 'one thousand and twelve', + 'resultThai' => 'หนึ่งพันสิบสอง' + ), + array( + 'number' => 1013, + 'resultEng' => 'one thousand and thirteen', + 'resultThai' => 'หนึ่งพันสิบสาม' + ), + array( + 'number' => 1100, + 'resultEng' => 'one thousand, one hundred', + 'resultThai' => 'หนึ่งพันหนึ่งร้อย' + ), + array( + 'number' => 10000, + 'resultEng' => 'ten thousand', + 'resultThai' => 'หนึ่งหมื่น' + ), + array( + 'number' => 10001, + 'resultEng' => 'ten thousand and one', + 'resultThai' => 'หนึ่งหมื่นเอ็ด' + ), + array( + 'number' => 10010, + 'resultEng' => 'ten thousand and ten', + 'resultThai' => 'หนึ่งหมื่นสิบ' + ), + array( + 'number' => 10011, + 'resultEng' => 'ten thousand and eleven', + 'resultThai' => 'หนึ่งหมื่นสิบเอ็ด' + ), + array( + 'number' => 12000, + 'resultEng' => 'twelve thousand', + 'resultThai' => 'หนึ่งหมื่นสองพัน' + ), + array( + 'number' => 100000, + 'resultEng' => 'one hundred thousand', + 'resultThai' => 'หนึ่งแสน' + ), + array( + 'number' => 100001, + 'resultEng' => 'one hundred thousand and one', + 'resultThai' => 'หนึ่งแสนเอ็ด' + ), + array( + 'number' => 100010, + 'resultEng' => 'one hundred thousand and ten', + 'resultThai' => 'หนึ่งแสนสิบ' + ), + array( + 'number' => 100011, + 'resultEng' => 'one hundred thousand and eleven', + 'resultThai' => 'หนึ่งแสนสิบเอ็ด' + ), + array( + 'number' => 100012, + 'resultEng' => 'one hundred thousand and twelve', + 'resultThai' => 'หนึ่งแสนสิบสอง' + ), + array( + 'number' => 100013, + 'resultEng' => 'one hundred thousand and thirteen', + 'resultThai' => 'หนึ่งแสนสิบสาม' + ), + array( + 'number' => 111111, + 'resultEng' => 'one hundred and eleven thousand, one hundred and eleven', + 'resultThai' => 'หนึ่งแสนหนึ่งหมื่นหนึ่งพันหนึ่งร้อยสิบเอ็ด' + ), + array( + 'number' => 1234567, + 'resultEng' => 'one million, two hundred and thirty-four thousand, five hundred and sixty-seven', + 'resultThai' => 'หนึ่งล้านสองแสนสามหมื่นสี่พันห้าร้อยหกสิบเจ็ด' + ), + array( + 'number' => 87654321, + 'resultEng' => 'eighty-seven million, six hundred and fifty-four thousand, three hundred and twenty-one', + 'resultThai' => 'แปดสิบเจ็ดล้านหกแสนห้าหมื่นสี่พันสามร้อยยี่สิบเอ็ด' + ), + array( + 'number' => 987654321, + 'resultEng' => 'nine hundred and eighty-seven million, six hundred and fifty-four thousand, three hundred and twenty-one', + 'resultThai' => 'เก้าร้อยแปดสิบเจ็ดล้านหกแสนห้าหมื่นสี่พันสามร้อยยี่สิบเอ็ด' + ), + array( + 'number' => 1987654321, + 'resultEng' => 'one billion, nine hundred and eighty-seven million, six hundred and fifty-four thousand, three hundred and twenty-one', + 'resultThai' => 'หนึ่งพันเก้าร้อยแปดสิบเจ็ดล้านหกแสนห้าหมื่นสี่พันสามร้อยยี่สิบเอ็ด' + ), + array( + 'number' => '12345678912', + 'resultEng' => 'twelve billion, three hundred and forty-five million, six hundred and seventy-eight thousand, nine hundred and twelve', + 'resultThai' => 'หนึ่งหมื่นสองพันสามร้อยสี่สิบห้าล้านหกแสนเจ็ดหมื่นแปดพันเก้าร้อยสิบสอง' + ), + array( + 'number' => '123456789123', + 'resultEng' => 'one hundred and twenty-three billion, four hundred and fifty-six million, seven hundred and eighty-nine thousand, one hundred and twenty-three', + 'resultThai' => 'หนึ่งแสนสองหมื่นสามพันสี่ร้อยห้าสิบหกล้านเจ็ดแสนแปดหมื่นเก้าพันหนึ่งร้อยยี่สิบสาม' + ), + array( + 'number' => '5554321987321', + 'resultEng' => 'five trillion, five hundred and fifty-four billion, three hundred and twenty-one million, nine hundred and eighty-seven thousand, three hundred and twenty-one', + 'resultThai' => 'ห้าล้านห้าแสนห้าหมื่นสี่พันสามร้อยยี่สิบเอ็ดล้านเก้าแสนแปดหมื่นเจ็ดพันสามร้อยยี่สิบเอ็ด' + ), + array( + 'number' => '100000000000000', + 'resultEng' => 'one hundred trillion', + 'resultThai' => 'หนึ่งร้อยล้านล้าน' + ), + array( + 'number' => '100000000000000000000', + 'resultEng' => 'one hundred quintrillion', + 'resultThai' => 'หนึ่งร้อยล้านล้านล้าน' + ), + array( + 'number' => '10000000000000000000000000', + 'resultEng' => 'ten septillion', + 'resultThai' => 'สิบล้านล้านล้านล้าน' + ), + array( + 'number' => '100000000000000000000000000', + 'resultEng' => 'one hundred septillion', + 'resultThai' => 'หนึ่งร้อยล้านล้านล้านล้าน' + ), + array( + 'number' => '1000000000000000000000000000', + 'resultEng' => 'one octillion', + 'resultThai' => 'หนึ่งพันล้านล้านล้านล้าน' + ), + array( + 'number' => '10000000000000000000000000000', + 'resultEng' => 'ten octillion', + 'resultThai' => 'หนึ่งหมื่นล้านล้านล้านล้าน' + ), + array( + 'number' => '100000000000000000000000000000', + 'resultEng' => 'one hundred octillion', + 'resultThai' => 'หนึ่งแสนล้านล้านล้านล้าน' + ), + array( + 'number' => '1000000000000000000000000000000', + 'resultEng' => 'one nonillion', + 'resultThai' => 'หนึ่งล้านล้านล้านล้านล้าน' + ), + array( + 'number' => '1000000000000000000000000000000000', + 'resultEng' => 'one decillion', + 'resultThai' => 'หนึ่งพันล้านล้านล้านล้านล้าน' + ), + array( + 'number' => '10000000000000000000000000000000000', + 'resultEng' => 'ten decillion', + 'resultThai' => 'หนึ่งหมื่นล้านล้านล้านล้านล้าน' + ), + array( + 'number' => '100000000000000000000000000000000000', + 'resultEng' => 'one hundred decillion', + 'resultThai' => 'หนึ่งแสนล้านล้านล้านล้านล้าน' + ), + array( + 'number' => '1000000000000000000000000000000000000', + 'resultEng' => 'Error!', + 'resultThai' => 'หนึ่งล้านล้านล้านล้านล้านล้าน' + ) + ); + + + public function testEngResult() + { + $Number = new \Rundiz\Number\Number(); + foreach ($this->numbersEng as $item) { + $this->assertEquals($item['resultEng'], $Number->convertNumber($item['number'], 'Eng')); + } + }// testEngResult + + + public function testThaiResult() + { + $Number = new \Rundiz\Number\Number(); + foreach ($this->numbersEng as $item) { + $this->assertEquals($item['resultThai'], $Number->convertNumber($item['number'], 'Thai')); + } + }// testThaiResult + + +} \ No newline at end of file diff --git a/tests/phpunit/ConvertTHBTest.php b/tests/phpunit/ConvertTHBTest.php new file mode 100644 index 0000000..d0bc7f3 --- /dev/null +++ b/tests/phpunit/ConvertTHBTest.php @@ -0,0 +1,408 @@ + -9210000000, + 'resultEng' => 'negative nine billion, two hundred and ten million Baht', + 'resultThai' => 'ลบเก้าพันสองร้อยสิบล้านบาทถ้วน' + ), + array( + 'number' => '-1000000', + 'resultEng' => 'negative one million Baht', + 'resultThai' => 'ลบหนึ่งล้านบาทถ้วน' + ), + array( + 'number' => '-100000', + 'resultEng' => 'negative one hundred thousand Baht', + 'resultThai' => 'ลบหนึ่งแสนบาทถ้วน' + ), + array( + 'number' => '-10000', + 'resultEng' => 'negative ten thousand Baht', + 'resultThai' => 'ลบหนึ่งหมื่นบาทถ้วน' + ), + array( + 'number' => -12, + 'resultEng' => 'negative twelve Baht', + 'resultThai' => 'ลบสิบสองบาทถ้วน' + ), + array( + 'number' => '-0.1234', + 'resultEng' => 'negative zero point one two three four Baht', + 'resultThai' => 'ลบศูนย์บาทหนึ่งพันสองร้อยสามสิบสี่สตางค์' + ), + array( + 'number' => '+5.45', + 'resultEng' => 'positive five point four five Baht', + 'resultThai' => 'บวกห้าบาทสี่สิบห้าสตางค์' + ), + array( + 'number' => 0, + 'resultEng' => 'zero Baht', + 'resultThai' => 'ศูนย์บาทถ้วน' + ), + array( + 'number' => 0.123456789, + 'resultEng' => 'zero point one two three four five six seven eight nine Baht', + 'resultThai' => 'ศูนย์บาทหนึ่งร้อยยี่สิบสามล้านสี่แสนห้าหมื่นหกพันเจ็ดร้อยแปดสิบเก้าสตางค์' + ), + array( + 'number' => 0.05, + 'resultEng' => 'zero point zero five Baht', + 'resultThai' => 'ศูนย์บาทห้าสตางค์' + ), + array( + 'number' => 0.2, + 'resultEng' => 'zero point two Baht', + 'resultThai' => 'ศูนย์บาทยี่สิบสตางค์' + ), + array( + 'number' => '0.20', + 'resultEng' => 'zero point two zero Baht', + 'resultThai' => 'ศูนย์บาทยี่สิบสตางค์' + ), + array( + 'number' => 0.23, + 'resultEng' => 'zero point two three Baht', + 'resultThai' => 'ศูนย์บาทยี่สิบสามสตางค์' + ), + array( + 'number' => 0.202, + 'resultEng' => 'zero point two zero two Baht', + 'resultThai' => 'ศูนย์บาทสองร้อยสองสตางค์' + ), + array( + 'number' => 0.99, + 'resultEng' => 'zero point nine nine Baht', + 'resultThai' => 'ศูนย์บาทเก้าสิบเก้าสตางค์' + ), + array( + 'number' => 0.2102, + 'resultEng' => 'zero point two one zero two Baht', + 'resultThai' => 'ศูนย์บาทสองพันหนึ่งร้อยสองสตางค์' + ), + array( + 'number' => '0212', + 'resultEng' => 'two hundred and twelve Baht', + 'resultThai' => 'สองร้อยสิบสองบาทถ้วน' + ), + array( + 'number' => '00213', + 'resultEng' => 'two hundred and thirteen Baht', + 'resultThai' => 'สองร้อยสิบสามบาทถ้วน' + ), + array( + 'number' => '1.00', + 'resultEng' => 'one Baht', + 'resultThai' => 'หนึ่งบาทถ้วน' + ), + array( + 'number' => 324.5, + 'resultEng' => 'three hundred and twenty-four point five Baht', + 'resultThai' => 'สามร้อยยี่สิบสี่บาทห้าสิบสตางค์' + ), + array( + 'number' => '324.50', + 'resultEng' => 'three hundred and twenty-four point five zero Baht', + 'resultThai' => 'สามร้อยยี่สิบสี่บาทห้าสิบสตางค์' + ), + array( + 'number' => 1, + 'resultEng' => 'one Baht', + 'resultThai' => 'หนึ่งบาทถ้วน' + ), + array( + 'number' => 5, + 'resultEng' => 'five Baht', + 'resultThai' => 'ห้าบาทถ้วน' + ), + array( + 'number' => 10, + 'resultEng' => 'ten Baht', + 'resultThai' => 'สิบบาทถ้วน' + ), + array( + 'number' => 11, + 'resultEng' => 'eleven Baht', + 'resultThai' => 'สิบเอ็ดบาทถ้วน' + ), + array( + 'number' => 12, + 'resultEng' => 'twelve Baht', + 'resultThai' => 'สิบสองบาทถ้วน' + ), + array( + 'number' => 13, + 'resultEng' => 'thirteen Baht', + 'resultThai' => 'สิบสามบาทถ้วน' + ), + array( + 'number' => 20, + 'resultEng' => 'twenty Baht', + 'resultThai' => 'ยี่สิบบาทถ้วน' + ), + array( + 'number' => 21, + 'resultEng' => 'twenty-one Baht', + 'resultThai' => 'ยี่สิบเอ็ดบาทถ้วน' + ), + array( + 'number' => 30, + 'resultEng' => 'thirty Baht', + 'resultThai' => 'สามสิบบาทถ้วน' + ), + array( + 'number' => 33, + 'resultEng' => 'thirty-three Baht', + 'resultThai' => 'สามสิบสามบาทถ้วน' + ), + array( + 'number' => 100, + 'resultEng' => 'one hundred Baht', + 'resultThai' => 'หนึ่งร้อยบาทถ้วน' + ), + array( + 'number' => 101, + 'resultEng' => 'one hundred and one Baht', + 'resultThai' => 'หนึ่งร้อยเอ็ดบาทถ้วน' + ), + array( + 'number' => 1000, + 'resultEng' => 'one thousand Baht', + 'resultThai' => 'หนึ่งพันบาทถ้วน' + ), + array( + 'number' => 1001, + 'resultEng' => 'one thousand and one Baht', + 'resultThai' => 'หนึ่งพันเอ็ดบาทถ้วน' + ), + array( + 'number' => 1010, + 'resultEng' => 'one thousand and ten Baht', + 'resultThai' => 'หนึ่งพันสิบบาทถ้วน' + ), + array( + 'number' => 1011, + 'resultEng' => 'one thousand and eleven Baht', + 'resultThai' => 'หนึ่งพันสิบเอ็ดบาทถ้วน' + ), + array( + 'number' => 1012, + 'resultEng' => 'one thousand and twelve Baht', + 'resultThai' => 'หนึ่งพันสิบสองบาทถ้วน' + ), + array( + 'number' => 1013, + 'resultEng' => 'one thousand and thirteen Baht', + 'resultThai' => 'หนึ่งพันสิบสามบาทถ้วน' + ), + array( + 'number' => 1100, + 'resultEng' => 'one thousand, one hundred Baht', + 'resultThai' => 'หนึ่งพันหนึ่งร้อยบาทถ้วน' + ), + array( + 'number' => 10000, + 'resultEng' => 'ten thousand Baht', + 'resultThai' => 'หนึ่งหมื่นบาทถ้วน' + ), + array( + 'number' => 10001, + 'resultEng' => 'ten thousand and one Baht', + 'resultThai' => 'หนึ่งหมื่นเอ็ดบาทถ้วน' + ), + array( + 'number' => 10010, + 'resultEng' => 'ten thousand and ten Baht', + 'resultThai' => 'หนึ่งหมื่นสิบบาทถ้วน' + ), + array( + 'number' => 10011, + 'resultEng' => 'ten thousand and eleven Baht', + 'resultThai' => 'หนึ่งหมื่นสิบเอ็ดบาทถ้วน' + ), + array( + 'number' => 12000, + 'resultEng' => 'twelve thousand Baht', + 'resultThai' => 'หนึ่งหมื่นสองพันบาทถ้วน' + ), + array( + 'number' => 100000, + 'resultEng' => 'one hundred thousand Baht', + 'resultThai' => 'หนึ่งแสนบาทถ้วน' + ), + array( + 'number' => 100001, + 'resultEng' => 'one hundred thousand and one Baht', + 'resultThai' => 'หนึ่งแสนเอ็ดบาทถ้วน' + ), + array( + 'number' => 100010, + 'resultEng' => 'one hundred thousand and ten Baht', + 'resultThai' => 'หนึ่งแสนสิบบาทถ้วน' + ), + array( + 'number' => 100011, + 'resultEng' => 'one hundred thousand and eleven Baht', + 'resultThai' => 'หนึ่งแสนสิบเอ็ดบาทถ้วน' + ), + array( + 'number' => 100012, + 'resultEng' => 'one hundred thousand and twelve Baht', + 'resultThai' => 'หนึ่งแสนสิบสองบาทถ้วน' + ), + array( + 'number' => 100013, + 'resultEng' => 'one hundred thousand and thirteen Baht', + 'resultThai' => 'หนึ่งแสนสิบสามบาทถ้วน' + ), + array( + 'number' => 111111, + 'resultEng' => 'one hundred and eleven thousand, one hundred and eleven Baht', + 'resultThai' => 'หนึ่งแสนหนึ่งหมื่นหนึ่งพันหนึ่งร้อยสิบเอ็ดบาทถ้วน' + ), + array( + 'number' => 1234567, + 'resultEng' => 'one million, two hundred and thirty-four thousand, five hundred and sixty-seven Baht', + 'resultThai' => 'หนึ่งล้านสองแสนสามหมื่นสี่พันห้าร้อยหกสิบเจ็ดบาทถ้วน' + ), + array( + 'number' => 87654321, + 'resultEng' => 'eighty-seven million, six hundred and fifty-four thousand, three hundred and twenty-one Baht', + 'resultThai' => 'แปดสิบเจ็ดล้านหกแสนห้าหมื่นสี่พันสามร้อยยี่สิบเอ็ดบาทถ้วน' + ), + array( + 'number' => 987654321, + 'resultEng' => 'nine hundred and eighty-seven million, six hundred and fifty-four thousand, three hundred and twenty-one Baht', + 'resultThai' => 'เก้าร้อยแปดสิบเจ็ดล้านหกแสนห้าหมื่นสี่พันสามร้อยยี่สิบเอ็ดบาทถ้วน' + ), + array( + 'number' => 1987654321, + 'resultEng' => 'one billion, nine hundred and eighty-seven million, six hundred and fifty-four thousand, three hundred and twenty-one Baht', + 'resultThai' => 'หนึ่งพันเก้าร้อยแปดสิบเจ็ดล้านหกแสนห้าหมื่นสี่พันสามร้อยยี่สิบเอ็ดบาทถ้วน' + ), + array( + 'number' => '12345678912', + 'resultEng' => 'twelve billion, three hundred and forty-five million, six hundred and seventy-eight thousand, nine hundred and twelve Baht', + 'resultThai' => 'หนึ่งหมื่นสองพันสามร้อยสี่สิบห้าล้านหกแสนเจ็ดหมื่นแปดพันเก้าร้อยสิบสองบาทถ้วน' + ), + array( + 'number' => '123456789123', + 'resultEng' => 'one hundred and twenty-three billion, four hundred and fifty-six million, seven hundred and eighty-nine thousand, one hundred and twenty-three Baht', + 'resultThai' => 'หนึ่งแสนสองหมื่นสามพันสี่ร้อยห้าสิบหกล้านเจ็ดแสนแปดหมื่นเก้าพันหนึ่งร้อยยี่สิบสามบาทถ้วน' + ), + array( + 'number' => '5554321987321', + 'resultEng' => 'five trillion, five hundred and fifty-four billion, three hundred and twenty-one million, nine hundred and eighty-seven thousand, three hundred and twenty-one Baht', + 'resultThai' => 'ห้าล้านห้าแสนห้าหมื่นสี่พันสามร้อยยี่สิบเอ็ดล้านเก้าแสนแปดหมื่นเจ็ดพันสามร้อยยี่สิบเอ็ดบาทถ้วน' + ), + array( + 'number' => '100000000000000', + 'resultEng' => 'one hundred trillion Baht', + 'resultThai' => 'หนึ่งร้อยล้านล้านบาทถ้วน' + ), + array( + 'number' => '100000000000000000000', + 'resultEng' => 'one hundred quintrillion Baht', + 'resultThai' => 'หนึ่งร้อยล้านล้านล้านบาทถ้วน' + ), + array( + 'number' => '10000000000000000000000000', + 'resultEng' => 'ten septillion Baht', + 'resultThai' => 'สิบล้านล้านล้านล้านบาทถ้วน' + ), + array( + 'number' => '100000000000000000000000000', + 'resultEng' => 'one hundred septillion Baht', + 'resultThai' => 'หนึ่งร้อยล้านล้านล้านล้านบาทถ้วน' + ), + array( + 'number' => '1000000000000000000000000000', + 'resultEng' => 'one octillion Baht', + 'resultThai' => 'หนึ่งพันล้านล้านล้านล้านบาทถ้วน' + ), + array( + 'number' => '10000000000000000000000000000', + 'resultEng' => 'ten octillion Baht', + 'resultThai' => 'หนึ่งหมื่นล้านล้านล้านล้านบาทถ้วน' + ), + array( + 'number' => '100000000000000000000000000000', + 'resultEng' => 'one hundred octillion Baht', + 'resultThai' => 'หนึ่งแสนล้านล้านล้านล้านบาทถ้วน' + ), + array( + 'number' => '1000000000000000000000000000000', + 'resultEng' => 'one nonillion Baht', + 'resultThai' => 'หนึ่งล้านล้านล้านล้านล้านบาทถ้วน' + ), + array( + 'number' => '1000000000000000000000000000000000', + 'resultEng' => 'one decillion Baht', + 'resultThai' => 'หนึ่งพันล้านล้านล้านล้านล้านบาทถ้วน' + ), + array( + 'number' => '10000000000000000000000000000000000', + 'resultEng' => 'ten decillion Baht', + 'resultThai' => 'หนึ่งหมื่นล้านล้านล้านล้านล้านบาทถ้วน' + ), + array( + 'number' => '100000000000000000000000000000000000', + 'resultEng' => 'one hundred decillion Baht', + 'resultThai' => 'หนึ่งแสนล้านล้านล้านล้านล้านบาทถ้วน' + ), + array( + 'number' => '1000000000000000000000000000000000000', + 'resultEng' => 'Error! Baht', + 'resultThai' => 'หนึ่งล้านล้านล้านล้านล้านล้านบาทถ้วน' + ) + ); + + + public function testEngResult() + { + $Number = new \Rundiz\Number\Number(); + foreach ($this->numbersEng as $item) { + $this->assertEquals($item['resultEng'], $Number->convertBaht($item['number'], true, 'Eng')); + } + }// testEngResult + + + public function testThaiResult() + { + $Number = new \Rundiz\Number\Number(); + foreach ($this->numbersEng as $item) { + $this->assertEquals($item['resultThai'], $Number->convertBaht($item['number'], true, 'Thai')); + } + + // test donot display net. + $this->assertEquals('ศูนย์บาท', $Number->convertBaht(0, false, 'Thai')); + $this->assertEquals('ห้าบาท', $Number->convertBaht(5, false, 'Thai')); + $this->assertEquals('ห้าบาท', $Number->convertBaht('5', false, 'Thai')); + $this->assertEquals('หนึ่งร้อยเอ็ดบาท', $Number->convertBaht(101, false, 'Thai')); + $this->assertEquals('หนึ่งแสนบาท', $Number->convertBaht(100000, false, 'Thai')); + $this->assertEquals('หนึ่งแสนเอ็ดบาท', $Number->convertBaht(100001, false, 'Thai')); + $this->assertEquals('หนึ่งแสนสิบบาท', $Number->convertBaht(100010, false, 'Thai')); + $this->assertEquals('หนึ่งล้านบาท', $Number->convertBaht(1000000, false, 'Thai')); + $this->assertEquals('สิบล้านบาท', $Number->convertBaht(10000000, false, 'Thai')); + $this->assertEquals('หนึ่งร้อยล้านบาท', $Number->convertBaht(100000000, false, 'Thai')); + $this->assertEquals('หนึ่งพันล้านบาท', $Number->convertBaht(1000000000, false, 'Thai')); + $this->assertEquals('หนึ่งหมื่นล้านบาท', $Number->convertBaht('10000000000', false, 'Thai')); + $this->assertEquals('หนึ่งแสนล้านบาท', $Number->convertBaht('100000000000', false, 'Thai')); + $this->assertEquals('หนึ่งล้านล้านบาท', $Number->convertBaht('1000000000000', false, 'Thai')); + $this->assertEquals('สิบล้านล้านบาท', $Number->convertBaht('10000000000000', false, 'Thai')); + $this->assertEquals('หนึ่งพันล้านล้านบาท', $Number->convertBaht('1000000000000000', false, 'Thai')); + $this->assertEquals('หนึ่งหมื่นล้านล้านบาท', $Number->convertBaht('10000000000000000', false, 'Thai')); + $this->assertEquals('หนึ่งร้อยล้านล้านบาท', $Number->convertBaht('100000000000000', false, 'Thai')); + $this->assertEquals('หนึ่งหมื่นล้านล้านล้านล้านล้านบาท', $Number->convertBaht('10000000000000000000000000000000000', false, 'Thai')); + }// testThaiResult + + +} \ No newline at end of file diff --git a/tests/phpunit/NumberTest.php b/tests/phpunit/NumberTest.php index 1117c01..45af200 100644 --- a/tests/phpunit/NumberTest.php +++ b/tests/phpunit/NumberTest.php @@ -7,37 +7,36 @@ class NumberTest extends \PHPUnit\Framework\TestCase { - public function testConvertBaht() - { - $Number = new \Rundiz\Number\Number(); - $this->assertEquals('negative forty-three million, two hundred and thirteen thousand, eight hundred and ninety-five Baht', $Number->convertBaht(-43213895, true, 'Eng')); - $this->assertEquals('ลบสี่สิบสามล้านสองแสนหนึ่งหมื่นสามพันแปดร้อยเก้าสิบห้าบาทถ้วน', $Number->convertBaht(-43213895, true, 'Thai')); - }// testConvertBaht - - - public function testConvertNumber() - { - $Number = new \Rundiz\Number\Number(); - $this->assertEquals('three hundred and twenty-four point five', $Number->convertNumber(324.50, 'Eng')); - $this->assertEquals('สามร้อยยี่สิบสี่จุดห้า', $Number->convertNumber(324.50, 'Thai')); - }// testConvertNumber - - public function testConvertByte() { $Number = new \Rundiz\Number\Number(); - $this->assertEquals('100.00 GB', $Number->fromBytes(100000000000)); + $this->assertEquals('10000.00 B', $Number->fromBytes(10000, 'INVALIDUNIT')); + $this->assertEquals('20.00 TB', $Number->fromBytes(20000000000001)); $this->assertEquals('123.45 GB', $Number->fromBytes(123456789123)); + $this->assertEquals('100.00 GB', $Number->fromBytes(100000000000)); + $this->assertEquals('10.00 MB', $Number->fromBytes(10000000)); + $this->assertEquals('100.00 KB', $Number->fromBytes(100000)); $this->assertEquals('123.45 B', $Number->fromBytes(123.456789123)); $this->assertFalse($Number->fromBytes(-123.456789123)); + $this->assertEquals('8796093022208', $Number->toBytes('8TiB')); + $this->assertEquals('8796093022208', $Number->toBytes('8.000TiB')); $this->assertEquals('8020000000000', $Number->toBytes('8.02TB')); + $this->assertEquals('1342177280', $Number->toBytes('1.25GiB')); + $this->assertEquals('132560717806.04314', $Number->toBytes('123.456789GiB')); $this->assertEquals('123456789000', $Number->toBytes('123.456789GB')); + $this->assertEquals('10171187.2', $Number->toBytes('9.7MiB')); + $this->assertEquals('9700000', $Number->toBytes('9.7MB')); + $this->assertEquals('1331.2', $Number->toBytes('1.3KiB')); + $this->assertEquals('1331.2', $Number->toBytes('1.30KiB')); $this->assertEquals('98765', $Number->toBytes('98.765KB')); $this->assertEquals('98765.43', $Number->toBytes('98.76543KB')); - $this->assertEquals('101135.80032', $Number->toBytes('98.76543KiB')); - $this->assertEquals('132560717806.04314', $Number->toBytes('123.456789GiB')); - $this->assertFalse($Number->toBytes('98.76543 KiB')); + $this->assertEquals('6960', $Number->toBytes('6960B')); + $this->assertEquals('1245', $Number->toBytes('1245')); + $this->assertEquals('2594', $Number->toBytes(2594)); + $this->assertFalse($Number->toBytes(45.60)); + $this->assertEquals('101135.80032', $Number->toBytes('98.76543KiB'));// not contain space. + $this->assertFalse($Number->toBytes('98.76543 KiB'));// contain space. $this->assertFalse($Number->toBytes('-123.456789GiB')); }// testConvertByte diff --git a/tests/via-http/_convert-numbers-set-test.php b/tests/via-http/_convert-numbers-set-test.php new file mode 100644 index 0000000..eb91f1d --- /dev/null +++ b/tests/via-http/_convert-numbers-set-test.php @@ -0,0 +1,31 @@ + $value) { + $r[] = "$indent " + . ($indexed ? '' : var_export54($key) . ' => ') + . var_export54($value, "$indent "); + } + return 'array(' . "\n" . implode(",\n", $r) . "\n" . $indent . ')'; + case 'boolean': + return $var ? 'TRUE' : 'FALSE'; + default: + return var_export($var, TRUE); + } +} \ No newline at end of file diff --git a/tests/via-http/convert-en.php b/tests/via-http/convert-en.php index 88e58b5..501ea63 100644 --- a/tests/via-http/convert-en.php +++ b/tests/via-http/convert-en.php @@ -2,24 +2,12 @@ require dirname(dirname(__DIR__)).'/Rundiz/Number/NumberEng.php'; -$numbers = array( - -9210000000, '-1000000', '-100000', '-10000', -12, '-0.1234', - 0.123456789, 0.05, 0.2, 0.99, - '1.00', - 1, 5, 10, 11, 12, 13, 20, 21, 30, 33, 101, 1000, 1001, 1010, 1011, 1012, 1013, 1100, 10000, 10001, 10010, 10011, 12000, 100000, 100001, 100010, 100011, 100012, 100013, 111111, - 1234567, 87654321, 987654321, 1987654321, 12345678912, 123456789123, 5554321987321, '5554321987321989898989465', '10000000000000000000000000', '100000000000000000000000000', - '1000000000000000000000000000', '10000000000000000000000000000', '100000000000000000000000000000', - '1000000000000000000000000000000', - '1000000000000000000000000000000000', - '900000000000000000000000000000000000', - '1000000000000000000000000000000000000', -); +require __DIR__ . '/_convert-numbers-set-test.php'; $numeng = new \Rundiz\Number\NumberEng(); foreach ($numbers as $num) { - echo $num.' = '.$numeng->convertNumber($num); - echo '
'; + echo $num.' = '.$numeng->convertNumber($num); + echo '
'; } -echo '
'; diff --git a/tests/via-http/convert-frombyte.php b/tests/via-http/convert-frombyte.php index c97bb13..b65b633 100644 --- a/tests/via-http/convert-frombyte.php +++ b/tests/via-http/convert-frombyte.php @@ -3,7 +3,7 @@ require dirname(dirname(__DIR__)).'/Rundiz/Number/Number.php'; $filesizes = array( - 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000, 10000000000, 100000000000, 1000000000000, 10000000000001, 100000000000000, 1000000000000000, 10000000000000000, 100000000000000000, 1000000000000000000, 10000000000000000000, + 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000, 10000000000, 200000000000, 1000000000000, 20000000000001, 100000000000000, 3000000000000000, 10000000000000000, 100000000000000000, 1000000000000000000, 10000000000000000000, 130, 1300, 13300, 133300, 1333300, 13333300, 133333300, 1333333300, 13333333300, 133333333300, 1333333333300, 13333333333300, ); diff --git a/tests/via-http/convert-number-shorter.php b/tests/via-http/convert-number-shorter.php index 14f5e35..58555e1 100644 --- a/tests/via-http/convert-number-shorter.php +++ b/tests/via-http/convert-number-shorter.php @@ -10,8 +10,11 @@ $numbers = array( -43213895, '-1000000', '-0.12345', - 0.987523, 0.05, 0.99, - '1.00', '21', 31, 1001, 100001, 324.50 + 0.987523, 0.05, 0.99, 0.2102, 0.0203, + '0212', '00213', + 1.02, 1.2, 1.20, '1.20', 1.202, + '1.00', '21', 31, 1001, 100001, 324.50, + '1000000000000000', ); diff --git a/tests/via-http/convert-th.php b/tests/via-http/convert-th.php index 8946d51..2ac10a1 100644 --- a/tests/via-http/convert-th.php +++ b/tests/via-http/convert-th.php @@ -2,25 +2,7 @@ require dirname(dirname(__DIR__)).'/Rundiz/Number/NumberThai.php'; -$numbers = array( - -9210000000, '-1000000', '-100000', '-10000', -12, '-0.1234', - 0, - 0.123456789, 0.05, 0.2, 0.23, 0.202, 0.99, - '1.00', - 1, 5, 10, 11, 12, 13, 20, 21, 30, 33, - 100, 101, 1000, 1001, 1010, 1011, 1012, 1013, 1100, - 10000, 10001, 10010, 10011, 12000, 100000, 100001, 100010, 100011, 100012, 100013, 111111, - 1234567, 87654321, 987654321, 1987654321, 12345678912, 123456789123, 5554321987321, - '100000000000000', - '100000000000000000000', - '10000000000000000000000000', '100000000000000000000000000', - '1000000000000000000000000000', '10000000000000000000000000000', '100000000000000000000000000000', - '1000000000000000000000000000000', - '1000000000000000000000000000000000', - '10000000000000000000000000000000000', - '100000000000000000000000000000000000', - '1000000000000000000000000000000000000', -); +require __DIR__ . '/_convert-numbers-set-test.php'; echo '' . PHP_EOL; @@ -31,4 +13,4 @@ } echo '
'; echo $numthai->arabicToThaiNumber('รอ 2 นาที 30 วินาที.').'
'; -echo $numthai->thaiToArabicNumber('รอ ๓ นาที ๔๕ วินาที.').'
'; +echo $numthai->thaiToArabicNumber('รอ ๓ นาที ๔๕ วินาที.').'
'; \ No newline at end of file diff --git a/tests/via-http/convert-thb.php b/tests/via-http/convert-thb.php index 2810b3f..c2b1179 100644 --- a/tests/via-http/convert-thb.php +++ b/tests/via-http/convert-thb.php @@ -2,31 +2,19 @@ require dirname(dirname(__DIR__)).'/Rundiz/Number/NumberThai.php'; -$numbers = array( - -9210000000, '-1000000', '-100000', '-10000', -12, '-0.1234', - 0, - 0.123456789, 0.05, 0.2, 0.23, 0.202, 0.99, - '1.00', - 1, 5, 10, 11, 12, 13, 20, 21, 30, 33, - 100, 101, 1000, 1001, 1010, 1011, 1012, 1013, 1100, - 10000, 10001, 10010, 10011, 12000, 100000, 100001, 100010, 100011, 100012, 100013, 111111, - 1234567, 87654321, 987654321, 1987654321, 12345678912, 123456789123, 5554321987321, - '100000000000000', - '100000000000000000000', - '10000000000000000000000000', '100000000000000000000000000', - '1000000000000000000000000000', '10000000000000000000000000000', '100000000000000000000000000000', - '1000000000000000000000000000000', - '1000000000000000000000000000000000', - '10000000000000000000000000000000000', - '100000000000000000000000000000000000', - '1000000000000000000000000000000000000', -); +require __DIR__ . '/_convert-numbers-set-test.php'; echo '' . PHP_EOL; $numthai = new \Rundiz\Number\NumberThai(); foreach ($numbers as $num) { - echo $num.' = '.$numthai->convertBaht($num); + echo $num . ' (' . gettype($num) . ') = '.$numthai->convertBaht($num); + echo '
'; +} + +echo '

Do not display net (ถ้วน)

'; +foreach ($numbers as $num) { + echo $num . ' (' . gettype($num) . ') = '.$numthai->convertBaht($num, false); echo '
'; } echo '
'; diff --git a/tests/via-http/convert-tobyte.php b/tests/via-http/convert-tobyte.php index 6e5f2fd..726a1b5 100644 --- a/tests/via-http/convert-tobyte.php +++ b/tests/via-http/convert-tobyte.php @@ -3,8 +3,10 @@ require dirname(dirname(__DIR__)).'/Rundiz/Number/Number.php'; $filesizes = array( + 45.60, + '98.76543 KiB', '98.76543KiB', 1000, '6960B', - '1.3KB', '1.3KiB', + '1.3KB', '1.3KiB', '1.30KiB', '9.7MB', '9.7MiB', '1.25GB', '1.25GiB', '8.02TB', '8.01TiB', @@ -16,6 +18,13 @@ $number = new Rundiz\Number\Number(); foreach ($filesizes as $size) { - echo $size.' = '.$number->toBytes($size).'
'; + echo $size . ' = '; + $converted = $number->toBytes($size); + if (is_numeric($converted)) { + echo $converted; + } else { + var_export($converted); + } + echo '
' . PHP_EOL; } unset($number); \ No newline at end of file diff --git a/tests/via-http/merge-convert-number-languages.php b/tests/via-http/merge-convert-number-languages.php new file mode 100644 index 0000000..2e7dc53 --- /dev/null +++ b/tests/via-http/merge-convert-number-languages.php @@ -0,0 +1,26 @@ +convertNumber($num); + $results[$i]['resultThai'] = $numthai->convertNumber($num); + $i++; +} + +unset($i, $numeng, $numthai); + + +echo '' . PHP_EOL; +echo '

For merge the results in Thai/English and use in PHPUnit test.

' . PHP_EOL; +echo '
'.var_export54($results).'
' . PHP_EOL; \ No newline at end of file diff --git a/tests/via-http/merge-convert-thb-languages.php b/tests/via-http/merge-convert-thb-languages.php new file mode 100644 index 0000000..217d41a --- /dev/null +++ b/tests/via-http/merge-convert-thb-languages.php @@ -0,0 +1,26 @@ +convertBaht($num, true, 'Eng'); + $results[$i]['resultThai'] = $Number->convertBaht($num); + $i++; +} + +unset($i, $Number); + + +echo '' . PHP_EOL; +echo '

For merge the results in Thai/English and use in PHPUnit test.

' . PHP_EOL; +echo '
'.var_export54($results).'
' . PHP_EOL; \ No newline at end of file