Example #1
0
 /**
  * Amount can be formatted to currency
  *
  * @test
  * @return void
  */
 public function it_can_format_amount_to_currency()
 {
     $amountOne = new Money(1200.9, 'USD');
     $this->assertEquals((string) $amountOne, '$1,200.90');
     $this->assertEquals($amountOne->format(), '$1,200.90');
     $currency = new Currency('USD');
     $currency->setPrecision(3);
     $currency->setThousandSeparator('.');
     $currency->setDecimalSeparator(',');
     $currency->setSymbolPlacement('after');
     $amountTwo = new Money(1200.9, $currency);
     $this->assertEquals((string) $amountTwo, '1.200,900$');
     $this->assertEquals($amountTwo->format(), '1.200,900$');
 }
Example #2
0
 /**
  * Get amount formatted to currency.
  *
  * @return mixed
  */
 public function amount()
 {
     // Indian Rupee use special format
     if ($this->currency->getCode() == 'INR') {
         $decimals = null;
         $amount = $this->amount;
         // Extract decimals from amount
         if (($pos = strpos($amount, ".")) !== false) {
             $decimals = substr(round(substr($amount, $pos), 2), 1);
             $amount = substr($amount, 0, $pos);
         }
         // Extract last 3 from amount
         $result = substr($amount, -3);
         $amount = substr($amount, 0, -3);
         // Apply digits 2 by 2
         while (strlen($amount) > 0) {
             $result = substr($amount, -2) . "," . $result;
             $amount = substr($amount, 0, -2);
         }
         return $result . $decimals;
     }
     // Return western format
     return number_format($this->amount, $this->currency->getPrecision(), $this->currency->getDecimalSeparator(), $this->currency->getThousandSeparator());
 }
Example #3
0
 /**
  * String can be parsed to numeric
  *
  * @test
  * @return void
  */
 public function it_can_get_all_currencies()
 {
     $this->assertTrue(is_array(Currency::getAllCurrencies()));
 }