Skip to content
This repository was archived by the owner on Dec 27, 2023. It is now read-only.

Commit 2f1d13e

Browse files
committed
Added ceil and floor methods
1 parent 83bb4e3 commit 2f1d13e

File tree

3 files changed

+74
-26
lines changed

3 files changed

+74
-26
lines changed

README.md

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ composer.json file the following text:
2828
```json
2929
{
3030
"require": {
31-
"litipk/php-bignumbers": "0.5.1"
31+
"litipk/php-bignumbers": "0.6.0"
3232
}
3333
}
3434
```
@@ -37,9 +37,9 @@ composer.json file the following text:
3737

3838
```php
3939
<?php
40-
40+
4141
use \Litipk\BigNumbers\Decimal as Decimal;
42-
42+
4343
/**
4444
* There are many ways to create Decimal objects.
4545
*
@@ -52,10 +52,10 @@ composer.json file the following text:
5252
*
5353
* Decimal::create // this method works as methods fromType, but is more flexible
5454
*/
55-
55+
5656
$ten = Decimal::fromInteger(10);
5757
$two = Decimal::fromString('2.0');
58-
58+
5959
/**
6060
* At this moment there are few binary operators
6161
* that we can use with Decimal objects:
@@ -78,7 +78,7 @@ composer.json file the following text:
7878
* $d1->round($scale);
7979
* $d1->additiveInverse();
8080
*/
81-
81+
8282
$five = Decimal::fromInteger(-5)->abs();
8383
$six = Decimal::fromInteger(6)->abs();
8484

@@ -98,7 +98,7 @@ composer.json file the following text:
9898
* $d1->isInfinite();
9999
* $d1->isNaN();
100100
*/
101-
101+
102102
$zero = Decimal::fromInteger(0);
103103
$zero->isZero(); // Returns true
104104

@@ -112,15 +112,3 @@ composer.json file the following text:
112112
The documentation is incomplete, if you want to use
113113
all the features of this package, you can see which
114114
public methods are declared in the Decimal class.
115-
116-
117-
## TODO List
118-
119-
- [ ] Create the **Integer** class.
120-
- [ ] Create the **Rational** class.
121-
- [ ] Create the **Complex** class.
122-
- [X] Add the *pow* method.
123-
124-
125-
[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/Litipk/php-bignumbers/trend.png)](https://bitdeli.com/free "Bitdeli Badge")
126-

src/Decimal.php

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,7 @@ public static function fromFloat($fltValue, $scale = null)
143143
$scale;
144144

145145
return new Decimal(
146-
number_format(
147-
$fltValue,
148-
$dec_scale,
149-
'.',
150-
''
151-
),
146+
number_format($fltValue, $dec_scale, '.', ''),
152147
$dec_scale
153148
);
154149
}
@@ -567,6 +562,51 @@ public function round($scale = 0)
567562
return self::fromString(self::innerRound($this->value, $scale));
568563
}
569564

565+
/**
566+
* "Ceils" the Decimal to have at most $scale digits after the point
567+
* @param integer $scale
568+
* @return Decimal
569+
*/
570+
public function ceil($scale = 0)
571+
{
572+
if ($scale >= $this->scale) {
573+
return $this;
574+
}
575+
576+
$rounded = bcadd($this->value, '0', $scale);
577+
578+
$rlen = strlen($rounded);
579+
$tlen = strlen($this->value);
580+
581+
$mustCeil = false;
582+
for ($i=$tlen-1; $i >= $rlen; $i--) {
583+
if ((int)$this->value[$i] > 0) {
584+
$mustCeil = true;
585+
break;
586+
}
587+
}
588+
589+
if ($mustCeil) {
590+
$rounded = bcadd($rounded, bcpow('10', -$scale, $scale), $scale);
591+
}
592+
593+
return self::fromString($rounded, $scale);
594+
}
595+
596+
/**
597+
* "Floors" the Decimal to have at most $scale digits after the point
598+
* @param integer $scale
599+
* @return Decimal
600+
*/
601+
public function floor($scale = 0)
602+
{
603+
if ($scale >= $this->scale) {
604+
return $this;
605+
}
606+
607+
return self::fromString(bcadd($this->value, '0', $scale));
608+
}
609+
570610
/**
571611
* Returns the absolute value (always a positive number)
572612
* @return Decimal
@@ -744,7 +784,7 @@ protected static function paramsValidation($value, $scale)
744784
/**
745785
* @return string
746786
*/
747-
private static function normalizeSign($sign)
787+
private static function normalizeSign($sign)
748788
{
749789
if ($sign==='+') {
750790
return '';

src/InfiniteDecimal.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,26 @@ public function round($scale = 0)
236236
return $this;
237237
}
238238

239+
/**
240+
* "Ceils" the Decimal to have at most $scale digits after the point
241+
* @param integer $scale
242+
* @return Decimal
243+
*/
244+
public function ceil($scale = 0)
245+
{
246+
return $this;
247+
}
248+
249+
/**
250+
* "Floors" the Decimal to have at most $scale digits after the point
251+
* @param integer $scale
252+
* @return Decimal
253+
*/
254+
public function floor($scale = 0)
255+
{
256+
return $this;
257+
}
258+
239259
/**
240260
* @return boolean
241261
*/

0 commit comments

Comments
 (0)