Skip to content

Commit

Permalink
support enforcing the number of decimals
Browse files Browse the repository at this point in the history
  • Loading branch information
lubosdz committed Aug 15, 2024
1 parent 9c229bc commit 7e1d3df
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 6 deletions.
8 changes: 8 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ NumberToWords_EN::convert(12.30); // dvanásť čiarka tridsať
NumberToWords::$decimalsAsFraction = true;
NumberToWords::convert(123.45, 'sk'); // jednostodvadsaťtri (45/100)

// enforce desired number of decimals
NumberToWords::$numberOfdecimals = 2;
NumberToWords::convert(12.3, 'sk'); // dvanásť celé tridsať

// Česky / Czech:
NumberToWords::convert(123.45, 'cz'); // allowed cz or cs, // sto dvacet tři celá čtyřicet pět
NumberToWords_CZ::convert(123.45); // sto dvacet tři celá čtyřicet pět
Expand Down Expand Up @@ -86,6 +90,10 @@ NumberToWords::convert(123.45, 'fr'); // cent vingt-trois virgule quatre cinq
Changelog
---------

1.0.6 - 15.08.2024
------------------
* support enforcing the number of decimals

1.0.5 - 10.08.2024
------------------
* make decimals separator word configurable
Expand Down
10 changes: 9 additions & 1 deletion src/NumberToWords.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,15 @@ class NumberToWords

/**
* @var string Output template when returning decimal part as a fraction (formatted with sprintf)
* First placeholder %s is for the fraction, second %s for the base ie. (99/100)
* First placeholder %s is for the fraction, second %s for the base ie. (99/100)
*/
public static $templateFraction = "(%s/%s)";

/**
* @var null|int Set the number of enforced decimals, must be > 0
*/
public static $numberOfdecimals = null;

/**
* Return supplied number as words
*
Expand All @@ -39,15 +44,18 @@ public static function convert($num, $lang = 'en', $forceIntl = false)
case 'sk':
NumberToWords_SK::$decimalsAsFraction = self::$decimalsAsFraction;
NumberToWords_SK::$templateFraction = self::$templateFraction;
NumberToWords_SK::$numberOfdecimals = self::$numberOfdecimals;
return $forceIntl ? NumberToWords_SK::convertIntl($num) : NumberToWords_SK::convert($num);
case 'cs':
case 'cz':
NumberToWords_CZ::$decimalsAsFraction = self::$decimalsAsFraction;
NumberToWords_CZ::$templateFraction = self::$templateFraction;
NumberToWords_CZ::$numberOfdecimals = self::$numberOfdecimals;
return $forceIntl ? NumberToWords_CZ::convertIntl($num) : NumberToWords_CZ::convert($num);
case 'en':
NumberToWords_EN::$decimalsAsFraction = self::$decimalsAsFraction;
NumberToWords_EN::$templateFraction = self::$templateFraction;
NumberToWords_EN::$numberOfdecimals = self::$numberOfdecimals;
return $forceIntl ? NumberToWords_EN::convertIntl($num) : NumberToWords_EN::convert($num);
default:
// not directly implemented language
Expand Down
10 changes: 9 additions & 1 deletion src/NumberToWords_CZ.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class NumberToWords_CZ

/**
* @var string Output template when returning decimal part as a fraction (formatted with sprintf)
* First placeholder %s is for the fraction, second %s for the base ie. (99/100)
* First placeholder %s is for the fraction, second %s for the base ie. (99/100)
*/
public static $templateFraction = "(%s/%s)";

Expand All @@ -31,6 +31,11 @@ class NumberToWords_CZ
*/
public static $txtDecimal = " celá ";

/**
* @var null|int Set the number of enforced decimals, must be > 0
*/
public static $numberOfdecimals = null;

/**
* Return converted number as a string
* @param float $number
Expand Down Expand Up @@ -101,6 +106,9 @@ public static function convert($number, $units = null, $level = -1)
$string = $fraction = '';

if (strpos($number, '.') !== false) {
if ( (int) self::$numberOfdecimals > 0 ) {
$number = number_format($number, (int) self::$numberOfdecimals, '.', '');
}
list($number, $fraction) = explode('.', $number);
}

Expand Down
10 changes: 9 additions & 1 deletion src/NumberToWords_EN.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class NumberToWords_EN

/**
* @var string Output template when returning decimal part as a fraction (formatted with sprintf)
* First placeholder %s is for the fraction, second %s for the base ie. (99/100)
* First placeholder %s is for the fraction, second %s for the base ie. (99/100)
*/
public static $templateFraction = "(%s/%s)";

Expand All @@ -31,6 +31,11 @@ class NumberToWords_EN
*/
public static $txtDecimal = " point ";

/**
* @var null|int Set the number of enforced decimals, must be > 0
*/
public static $numberOfdecimals = null;

/**
* Return converted number as a string
* @param float $number
Expand Down Expand Up @@ -101,6 +106,9 @@ public static function convert($number, $units = null, $level = -1)
$string = $fraction = '';

if (strpos($number, '.') !== false) {
if ( (int) self::$numberOfdecimals > 0 ) {
$number = number_format($number, (int) self::$numberOfdecimals, '.', '');
}
list($number, $fraction) = explode('.', $number);
}

Expand Down
8 changes: 8 additions & 0 deletions src/NumberToWords_SK.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ class NumberToWords_SK
*/
public static $txtDecimal = " celé ";

/**
* @var null|int Set the number of enforced decimals, must be > 0
*/
public static $numberOfdecimals = null;

/**
* Return converted number as a string
* @param float $number
Expand Down Expand Up @@ -102,6 +107,9 @@ public static function convert($number, $units = null, $level = -1)
$string = $fraction = '';

if (strpos($number, '.') !== false) {
if ( (int) self::$numberOfdecimals > 0 ) {
$number = number_format($number, (int) self::$numberOfdecimals, '.', '');
}
list($number, $fraction) = explode('.', $number);
}

Expand Down
59 changes: 56 additions & 3 deletions tests/NumberToWords_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,35 @@ public function testSK()
$this->assertTrue(false !== strpos($res_A, 'osemdesiat') && false !== strpos($res_A, 'čiarka jeden dva tri'));

// arbitrary decimals separator word
NumberToWords_SK::$txtDecimal = " čiarka ";
$num = "12.30";
NumberToWords_SK::$txtDecimal = " čiarka ";
$res_A = NumberToWords_SK::convert($num);
$this->assertTrue($res_A == 'dvanásť čiarka tridsať');

// enforce the number of decimals
NumberToWords_SK::$numberOfdecimals = 3;
$res_A = NumberToWords_SK::convert($num);
$this->assertTrue($res_A == 'dvanásť čiarka tristo');

NumberToWords_SK::$numberOfdecimals = 1;
NumberToWords_SK::$txtDecimal = " celé ";
$res_A = NumberToWords_SK::convert($num);
$this->assertTrue($res_A == 'dvanásť celé tri');

NumberToWords_SK::$numberOfdecimals = 0; // ignored, must be > 0
$res_A = NumberToWords_SK::convert($num);
$this->assertTrue($res_A == 'dvanásť celé tridsať');

NumberToWords::$numberOfdecimals = 4; // global / factory class
$res_A = NumberToWords::convert($num, 'sk');
$this->assertTrue($res_A == 'dvanásť celé tri nula nula nula');

NumberToWords::$numberOfdecimals = 2; // global / factory class
$res_A = NumberToWords::convert(12.3, 'sk');
$this->assertTrue($res_A == 'dvanásť celé tridsať');

// restore default value since this is static member, or some test might fail
NumberToWords::$numberOfdecimals = null;
}

// Czech tests
Expand Down Expand Up @@ -144,10 +169,24 @@ public function testCZ()
$this->assertTrue(false !== strpos($res_A, 'osmdesát sedm') && false !== strpos($res_A, 'čárka jeden dva tři'));

// arbitrary decimals separator word
NumberToWords_CZ::$txtDecimal = " čárka ";
$num = "12.30";
NumberToWords_CZ::$txtDecimal = " čárka ";
$res_A = NumberToWords_CZ::convert($num);
$this->assertTrue($res_A == 'dvanáct čárka třicet');

// enforce the number of decimals
NumberToWords_CZ::$numberOfdecimals = 3;
$res_A = NumberToWords_CZ::convert($num);
$this->assertTrue($res_A == 'dvanáct čárka tři sta');

NumberToWords_CZ::$numberOfdecimals = 1;
NumberToWords_CZ::$txtDecimal = " celá ";
$res_A = NumberToWords_CZ::convert($num);
$this->assertTrue($res_A == 'dvanáct celá tři');

NumberToWords_CZ::$numberOfdecimals = 0; // ignored, must be > 0
$res_A = NumberToWords_CZ::convert($num);
$this->assertTrue($res_A == 'dvanáct celá třicet');
}

// English + other lang tests
Expand Down Expand Up @@ -213,11 +252,25 @@ public function testEN()
$this->assertTrue(false !== strpos($res_A, 'seven million') && false !== strpos($res_A, 'point one two three'));

// arbitrary decimals separator word
NumberToWords_EN::$txtDecimal = " comma ";
$num = "12.30";
NumberToWords_EN::$txtDecimal = " comma ";
$res_A = NumberToWords_EN::convert($num);
$this->assertTrue($res_A == 'twelve comma thirty');

// enforce the number of decimals
NumberToWords_EN::$numberOfdecimals = 3;
$res_A = NumberToWords_EN::convert($num);
$this->assertTrue($res_A == 'twelve comma three hundred');

NumberToWords_EN::$numberOfdecimals = 1;
NumberToWords_EN::$txtDecimal = " EUR ";
$res_A = NumberToWords_EN::convert($num);
$this->assertTrue($res_A == 'twelve EUR three');

NumberToWords_EN::$numberOfdecimals = 0; // ignored, must be > 0
$res_A = NumberToWords_EN::convert($num);
$this->assertTrue($res_A == 'twelve EUR thirty');

// other langs ....

// Russian
Expand Down

0 comments on commit 7e1d3df

Please sign in to comment.