mirror of
				https://github.com/yiisoft/yii2.git
				synced 2025-11-04 14:46:19 +08:00 
			
		
		
		
	fixed currency format with custom decimal fraction
fixes #12345 close #12648
This commit is contained in:
		
				
					committed by
					
						
						Carsten Brandt
					
				
			
			
				
	
			
			
			
						parent
						
							a51dadc866
						
					
				
				
					commit
					50f3012f68
				
			@ -101,6 +101,7 @@ Yii Framework 2 Change Log
 | 
				
			|||||||
-----------------------
 | 
					-----------------------
 | 
				
			||||||
 | 
					
 | 
				
			||||||
- Bug #7670: Added `yii\web\UrlNormalizer` for normalizing requests with and without trailing slashes (rob006, cronfy, klimov-paul)
 | 
					- Bug #7670: Added `yii\web\UrlNormalizer` for normalizing requests with and without trailing slashes (rob006, cronfy, klimov-paul)
 | 
				
			||||||
 | 
					- Bug #12345 Fixed `Formatter::asCurrency()` for proper decimal formatting (Oxyaction)
 | 
				
			||||||
- Bug #7670: Added `UrlNormalizer` for normalizing requests with and without trailing slashes (rob006, cronfy, klimov-paul)
 | 
					- Bug #7670: Added `UrlNormalizer` for normalizing requests with and without trailing slashes (rob006, cronfy, klimov-paul)
 | 
				
			||||||
- Bug #9027: Fixed descendant class of `yii\web\UploadedFile` returns parent instances in case invoked after it (andrewnester)
 | 
					- Bug #9027: Fixed descendant class of `yii\web\UploadedFile` returns parent instances in case invoked after it (andrewnester)
 | 
				
			||||||
- Bug #9277: Fixed `yii\console\controllers\AssetController` looses custom options of 'target' bundles (petrabarus, klimov-paul)
 | 
					- Bug #9277: Fixed `yii\console\controllers\AssetController` looses custom options of 'target' bundles (petrabarus, klimov-paul)
 | 
				
			||||||
 | 
				
			|||||||
@ -1055,17 +1055,19 @@ class Formatter extends Component
 | 
				
			|||||||
        $value = $this->normalizeNumericValue($value);
 | 
					        $value = $this->normalizeNumericValue($value);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if ($this->_intlLoaded) {
 | 
					        if ($this->_intlLoaded) {
 | 
				
			||||||
 | 
					            $currency = $currency ?: $this->currencyCode;
 | 
				
			||||||
 | 
					            // currency code must be set before fraction digits
 | 
				
			||||||
 | 
					            // http://php.net/manual/en/numberformatter.formatcurrency.php#114376
 | 
				
			||||||
 | 
					            if ($currency && !isset($textOptions[NumberFormatter::CURRENCY_CODE])) {
 | 
				
			||||||
 | 
					                $textOptions[NumberFormatter::CURRENCY_CODE] = $currency;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
            $formatter = $this->createNumberFormatter(NumberFormatter::CURRENCY, null, $options, $textOptions);
 | 
					            $formatter = $this->createNumberFormatter(NumberFormatter::CURRENCY, null, $options, $textOptions);
 | 
				
			||||||
            if ($currency === null) {
 | 
					            if ($currency === null) {
 | 
				
			||||||
                if ($this->currencyCode === null) {
 | 
					                $result = $formatter->format($value);
 | 
				
			||||||
                    if (($result = $formatter->format($value)) === false) {
 | 
					            } else {
 | 
				
			||||||
                        throw new InvalidParamException('Formatting currency value failed: ' . $formatter->getErrorCode() . ' ' . $formatter->getErrorMessage());
 | 
					                $result = $formatter->formatCurrency($value, $currency);
 | 
				
			||||||
                    }
 | 
					 | 
				
			||||||
                    return $result;
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
                $currency = $this->currencyCode;
 | 
					 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            if (($result = $formatter->formatCurrency($value, $currency)) === false) {
 | 
					            if ($result === false) {
 | 
				
			||||||
                throw new InvalidParamException('Formatting currency value failed: ' . $formatter->getErrorCode() . ' ' . $formatter->getErrorMessage());
 | 
					                throw new InvalidParamException('Formatting currency value failed: ' . $formatter->getErrorCode() . ' ' . $formatter->getErrorMessage());
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            return $result;
 | 
					            return $result;
 | 
				
			||||||
 | 
				
			|||||||
@ -305,6 +305,15 @@ class FormatterNumberTest extends TestCase
 | 
				
			|||||||
        $this->assertSame("0,00 €", $this->formatter->asCurrency(false));
 | 
					        $this->assertSame("0,00 €", $this->formatter->asCurrency(false));
 | 
				
			||||||
        $this->assertSame("0,00 €", $this->formatter->asCurrency(""));
 | 
					        $this->assertSame("0,00 €", $this->formatter->asCurrency(""));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        // decimal formatting
 | 
				
			||||||
 | 
					        $this->formatter->locale = 'de-DE';
 | 
				
			||||||
 | 
					        $this->assertSame("100 $", \Yii::$app->formatter->asCurrency(100, 'USD', [
 | 
				
			||||||
 | 
					            NumberFormatter::MAX_FRACTION_DIGITS => 0,
 | 
				
			||||||
 | 
					        ]));
 | 
				
			||||||
 | 
					        $this->assertSame("100,00 $", $this->formatter->asCurrency(100, 'USD', [
 | 
				
			||||||
 | 
					            NumberFormatter::MAX_FRACTION_DIGITS => 2
 | 
				
			||||||
 | 
					        ]));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // null display
 | 
					        // null display
 | 
				
			||||||
        $this->assertSame($this->formatter->nullDisplay, $this->formatter->asCurrency(null));
 | 
					        $this->assertSame($this->formatter->nullDisplay, $this->formatter->asCurrency(null));
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user