@@ -154,7 +154,7 @@ class NumberFormatter
154
154
private $ attributes = array (
155
155
self ::FRACTION_DIGITS => 0 ,
156
156
self ::GROUPING_USED => 1 ,
157
- self ::ROUNDING_MODE => self ::ROUND_HALFEVEN
157
+ self ::ROUNDING_MODE => self ::ROUND_HALFEVEN ,
158
158
);
159
159
160
160
/**
@@ -171,7 +171,7 @@ class NumberFormatter
171
171
*/
172
172
private static $ supportedStyles = array (
173
173
'CURRENCY ' => self ::CURRENCY ,
174
- 'DECIMAL ' => self ::DECIMAL
174
+ 'DECIMAL ' => self ::DECIMAL ,
175
175
);
176
176
177
177
/**
@@ -182,7 +182,7 @@ class NumberFormatter
182
182
private static $ supportedAttributes = array (
183
183
'FRACTION_DIGITS ' => self ::FRACTION_DIGITS ,
184
184
'GROUPING_USED ' => self ::GROUPIN
8000
G_USED ,
185
- 'ROUNDING_MODE ' => self ::ROUNDING_MODE
185
+ 'ROUNDING_MODE ' => self ::ROUNDING_MODE ,
186
186
);
187
187
188
188
/**
@@ -195,7 +195,11 @@ class NumberFormatter
195
195
private static $ roundingModes = array (
196
196
'ROUND_HALFEVEN ' => self ::ROUND_HALFEVEN ,
197
197
'ROUND_HALFDOWN ' => self ::ROUND_HALFDOWN ,
198
- 'ROUND_HALFUP ' => self ::ROUND_HALFUP
198
+ 'ROUND_HALFUP ' => self ::ROUND_HALFUP ,
199
+ 'ROUND_CEILING ' => self ::ROUND_CEILING ,
200
+ 'ROUND_FLOOR ' => self ::ROUND_FLOOR ,
201
+ 'ROUND_DOWN ' => self ::ROUND_DOWN ,
202
+ 'ROUND_UP ' => self ::ROUND_UP ,
199
203
);
200
204
201
205
/**
@@ -209,7 +213,21 @@ class NumberFormatter
209
213
private static $ phpRoundingMap = array (
210
214
self ::ROUND_HALFDOWN => \PHP_ROUND_HALF_DOWN ,
211
215
self ::ROUND_HALFEVEN => \PHP_ROUND_HALF_EVEN ,
212
- self ::ROUND_HALFUP => \PHP_ROUND_HALF_UP
216
+ self ::ROUND_HALFUP => \PHP_ROUND_HALF_UP ,
217
+ );
218
+
219
+ /**
220
+ * The list of supported rounding modes which aren't available modes in
221
+ * PHP's round() function, but there's an equivalent. Keys are rounding
222
+ * modes, values does not matter.
223
+ *
224
+ * @var array
225
+ */
226
+ private static $ customRoundingList = array (
227
+ self ::ROUND_CEILING => true ,
228
+ self ::ROUND_FLOOR => true ,
229
+ self ::ROUND_DOWN => true ,
230
+ self ::ROUND_UP => true ,
213
231
);
214
232
215
233
/**
@@ -219,7 +237,7 @@ class NumberFormatter
219
237
*/
220
238
private static $ int32Range = array (
221
239
'positive ' => 2147483647 ,
222
- 'negative ' => -2147483648
240
+ 'negative ' => -2147483648 ,
223
241
);
224
242
225
243
/**
@@ -229,7 +247,7 @@ class NumberFormatter
229
247
*/
230
248
private static $ int64Range = array (
231
249
'positive ' => 9223372036854775807 ,
232
- 'negative ' => -9223372036854775808
250
+ 'negative ' => -9223372036854775808 ,
233
251
);
234
252
235
253
private static $ enSymbols = array (
@@ -505,7 +523,7 @@ public function parseCurrency($value, &$currency, &$position = null)
505
523
* Parse a number
506
524
*
507
525
* @param string $value The value to parse
508
- * @param string $type Type of the formatting, one of the format type constants. NumberFormatter::TYPE_DOUBLE by default
526
+ * @param int $type Type of the formatting, one of the format type constants. NumberFormatter::TYPE_DOUBLE by default
509
527
* @param int $position Offset to begin the parsing on return this value will hold the offset at which the parsing ended
510
528
*
511
529
* @return Boolean|string The parsed value of false on error
@@ -549,9 +567,7 @@ public function parse($value, $type = self::TYPE_DOUBLE, &$position = 0)
549
567
* @param int $attr An attribute specifier, one of the numeric attribute constants.
550
568
* The only currently supported attributes are NumberFormatter::FRACTION_DIGITS,
551
569
* NumberFormatter::GROUPING_USED and NumberFormatter::ROUNDING_MODE.
552
- * @param int $value The attribute value. The only currently supported rounding modes are
553
- * NumberFormatter::ROUND_HALFEVEN, NumberFormatter::ROUND_HALFDOWN and
554
- * NumberFormatter::ROUND_HALFUP.
570
+ * @param int $value The attribute value.
555
571
*
556
572
* @return Boolean true on success or false on failure
557
573
*
@@ -700,10 +716,32 @@ private function roundCurrency($value, $currency)
700
716
*/
701
717
private function round ($ value , $ precision )
702
718
{
703
- $ precision = $ this ->getUnitializedPrecision ($ value , $ precision );
719
+ $ precision = $ this ->getUninitializedPrecision ($ value , $ precision );
720
+
721
+ $ roundingModeAttribute = $ this ->getAttribute (self ::ROUNDING_MODE );
722
+ if (isset (self ::$ phpRoundingMap [$ roundingModeAttribute ])) {
723
+ $ value = round ($ value , $ precision , self ::$ phpRoundingMap [$ roundingModeAttribute ]);
724
+ } elseif (isset (self ::$ customRoundingList [$ roundingModeAttribute ])) {
725
+ $ roundingCoef = pow (10 , $ precision );
726
+ $ value *= $ roundingCoef ;
727
+
728
+ switch ($ roundingModeAttribute ) {
729
+ case self ::ROUND_CEILING :
730
+ $ value = ceil ($ value );
731
+ break ;
732
+ case self ::ROUND_FLOOR :
733
+ $ value = floor ($ value );
734
+ break ;
735
+ case self ::ROUND_UP :
736
+ $ value = $ value > 0 ? ceil ($ value ) : floor ($ value );
737
+ break ;
738
+ case self ::ROUND_DOWN :
739
+ $ value = $ value > 0 ? floor ($ value ) : ceil ($ value );
740
+ break ;
741
+ }
704
742
705
- $ roundingMode = self :: $ phpRoundingMap [ $ this -> getAttribute ( self :: ROUNDING_MODE )] ;
706
- $ value = round ( $ value , $ precision , $ roundingMode );
743
+ $ value /= $ roundingCoef ;
744
+ }
707
745
708
746
return $ value ;
709
747
}
@@ -718,20 +756,20 @@ private function round($value, $precision)
718
756
*/
719
757
private function formatNumber ($ value , $ precision )
720
758
{
721
- $ precision = $ this ->getUnitializedPrecision ($ value , $ precision );
759
+ $ precision = $ this ->getUninitializedPrecision ($ value , $ precision );
722
760
723
761
return number_format ($ value , $ precision , '. ' , $ this ->getAttribute (self ::GROUPING_USED ) ? ', ' : '' );
724
762
}
725
763
726
764
/**
727
- * Returns the precision value if the DECIMAL style is being used and the FRACTION_DIGITS attribute is unitialized .
765
+ * Returns the precision value if the DECIMAL style is being used and the FRACTION_DIGITS attribute is uninitialized .
728
766
*
729
- * @param integer|float $value The value to get the precision from if the FRACTION_DIGITS attribute is unitialized
767
+ * @param integer|float $value The value to get the precision from if the FRACTION_DIGITS attribute is uninitialized
730
768
* @param int $precision The precision value to returns if the FRACTION_DIGITS attribute is initialized
731
769
*
<
10000
/tr>732
770
* @return int The precision value
733
771
*/
734
- private function getUnitializedPrecision ($ value , $ precision )
772
+ private function getUninitializedPrecision ($ value , $ precision )
735
773
{
736
774
if ($ this ->style == self ::CURRENCY ) {
737
775
return $ precision ;
0 commit comments