Skip to content

Commit

Permalink
more strict tests + handle negative zero
Browse files Browse the repository at this point in the history
  • Loading branch information
rtlopez committed Mar 23, 2020
1 parent 44bd5a2 commit 13ab8bb
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 33 deletions.
12 changes: 10 additions & 2 deletions src/RtLopez/Decimal.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ public function format($prec = null, $dec_point = null , $thousands_sep = null,
if(($int_len - $i) % 3 == 0) array_unshift($int_str, $thousands_sep);
}
$int_str = implode('', $int_str);
$int_str = trim($int_str, $thousands_sep);

// format decimal part
$dec_len = strlen($decs);
Expand All @@ -158,9 +159,16 @@ public function format($prec = null, $dec_point = null , $thousands_sep = null,
{
$dec_str[$i] = $decs[$i];
}


// don't display "-0" nor "-0.0"
if($sign === '-' && $int_str === '0' && $dec_str == 0)
{
$sign = '';
}

// connect all parts
$number = $sign . trim($int_str . $dec_point . $dec_str, " $dec_point$thousands_sep");
$number = $sign . $int_str . $dec_point . $dec_str;

if($trailing_zero) return $number;

return $this->_trim($number);
Expand Down
80 changes: 49 additions & 31 deletions tests/FormatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,27 @@ public function providerToString()
$result = array();
foreach(self::$_classes as $class)
{
$result[] = array($class, 1, 0.0, '0');
$result[] = array($class, 1, 0.1, '0.1');
$result[] = array($class, 1, 0.01, '0');
$result[] = array($class, 1, 1.04, '1');
$result[] = array($class, 1, 0.05, '0.1');
$result[] = array($class, 1, 0.06, '0.1');
$result[] = array($class, 1, 0.09, '0.1');
$result[] = array($class, 1, -1.0, '-1');
$result[] = array($class, 2, '-2.001', '-2');
$result[] = array($class, 2, '-2.001', '-2');
$result[] = array($class, 2, '-2.004', '-2');
$result[] = array($class, 2, '-2.005', '-2.01');
$result[] = array($class, 2, '-2.006', '-2.01');
$result[] = array($class, 2, '-2.009', '-2.01');
$result[] = array($class, 3, '-2.0999', '-2.1');
$result[] = array($class, 3, '-2.00049', '-2.00');
$result[] = array($class, 3, '-2.00049', '-2.00');
$result[] = array($class, 3, '12.049', '12.049');
$result[] = array($class, 3, '123.049', '123.049');
$result[] = array($class, 6, '1234.00049', '1 234.000490');
$result[] = array($class, 9, '12345.1234567', '12 345.123456700');
$result[] = array($class, 1, 0.0, '0.0');
$result[] = array($class, 1, 0.1, '0.1');
$result[] = array($class, 1, 0.01, '0.0');
$result[] = array($class, 1, 1.04, '1.0');
$result[] = array($class, 1, 0.05, '0.1');
$result[] = array($class, 1, 0.06, '0.1');
$result[] = array($class, 1, 0.09, '0.1');
$result[] = array($class, 1, -1.0, '-1.0');
$result[] = array($class, 2, '-2.001', '-2.00');
$result[] = array($class, 2, '-2.001', '-2.00');
$result[] = array($class, 2, '-2.004', '-2.00');
$result[] = array($class, 2, '-2.005', '-2.01');
$result[] = array($class, 2, '-2.006', '-2.01');
$result[] = array($class, 2, '-2.009', '-2.01');
$result[] = array($class, 3, '-2.0999', '-2.100');
$result[] = array($class, 3, '-2.00049', '-2.000');
$result[] = array($class, 3, '-2.00049', '-2.000');
$result[] = array($class, 3, '12.049', '12.049');
$result[] = array($class, 3, '123.049', '123.049');
$result[] = array($class, 6, '1234.00049', '1 234.000490');
$result[] = array($class, 9, '12345.1234567', '12 345.123456700');
$result[] = array($class, 9, '123456.123456789', '123 456.123456789');
$result[] = array($class, 9, '1234567.123456789', '1 234 567.123456789');
//$result[] = array($class, 8, '12345678.123456788', '12 345 678.12345679');
Expand All @@ -52,7 +52,7 @@ public function providerToString()
public function testToString($class, $prec, $val, $exp)
{
$res = new $class($val, $prec);
$this->assertEquals($exp, $res->format());
$this->assertSame($exp, $res->format());
}

public function providerClasses()
Expand All @@ -71,7 +71,7 @@ public function providerClasses()
public function testFormatNoParams($class)
{
$res = new $class('1234.5678', 4);
$this->assertEquals('1 234.5678', $res->format());
$this->assertSame('1 234.5678', $res->format());
}

/**
Expand All @@ -80,7 +80,7 @@ public function testFormatNoParams($class)
public function testFormatPrec($class)
{
$res = new $class('1234.5678', 4);
$this->assertEquals('1 234.57', $res->format(2));
$this->assertSame('1 234.57', $res->format(2));
}

/**
Expand All @@ -89,7 +89,7 @@ public function testFormatPrec($class)
public function testFormatDecPoint($class)
{
$res = new $class('1234.5678', 4);
$this->assertEquals('1 234,5678', $res->format(null, ','));
$this->assertSame('1 234,5678', $res->format(null, ','));
}

/**
Expand All @@ -98,7 +98,7 @@ public function testFormatDecPoint($class)
public function testFormatEmptyThousands($class)
{
$res = new $class('1234.5678', 4);
$this->assertEquals('1234.5678', $res->format(null, '.', ''));
$this->assertSame('1234.5678', $res->format(null, '.', ''));
}

/**
Expand All @@ -107,7 +107,7 @@ public function testFormatEmptyThousands($class)
public function testFormatCommaThousands($class)
{
$res = new $class('1234.5678', 4);
$this->assertEquals('1,234.5678', $res->format(null, '.', ','));
$this->assertSame('1,234.5678', $res->format(null, '.', ','));
}

/**
Expand All @@ -116,7 +116,7 @@ public function testFormatCommaThousands($class)
public function testFormatWithoutCommaThousands($class)
{
$res = new $class('234.5678', 4);
$this->assertEquals('234.5678', $res->format(null, '.', ','));
$this->assertSame('234.5678', $res->format(null, '.', ','));
}

/**
Expand All @@ -125,7 +125,7 @@ public function testFormatWithoutCommaThousands($class)
public function testFormatDecimalAndThousands($class)
{
$res = new $class('1234.5678', 4);
$this->assertEquals('1--234--568', $res->format(3, '--', '--'));
$this->assertSame('1--234--568', $res->format(3, '--', '--'));
}

/**
Expand All @@ -134,7 +134,7 @@ public function testFormatDecimalAndThousands($class)
public function testFormatTrailingZero($class)
{
$res = new $class('1234.5', 4);
$this->assertEquals('1 234.500', $res->format(3));
$this->assertSame('1 234.500', $res->format(3));
}

/**
Expand All @@ -143,6 +143,24 @@ public function testFormatTrailingZero($class)
public function testFormatNoTrailingZero($class)
{
$res = new $class('1234', 4);
$this->assertEquals('1,234', $res->format(null, '.', ',', false));
$this->assertSame('1,234', $res->format(null, '.', ',', false));
}

/**
* @dataProvider providerClasses
*/
public function testFormatFractionOnly($class)
{
$res = new $class('0.5678', 4);
$this->assertSame('0.5678', $res->format());
}

/**
* @dataProvider providerClasses
*/
public function testFormatNoNegativeZero($class)
{
$res = new $class('-0.0001', 4);
$this->assertSame('0.00', $res->format(2));
}
}

0 comments on commit 13ab8bb

Please sign in to comment.