Skip to content

Commit

Permalink
format amount
Browse files Browse the repository at this point in the history
Previously, figures gotten from converting a single-unit fiat currency
to bitcoin were unreadable; mostly in the format `7.0297483496097E-8`
or `3.5258494994032E-5`. This commit will now render such figures in a
human readable format while taking into account, the significant figures
even in a 10th negative exponent!
  • Loading branch information
AndikanGabriel committed Jun 10, 2021
1 parent ff666f7 commit 0bd71e2
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 11 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ use Coindesk;
*/
Coindesk::toFiatCurrency($currency_code, $bitcoin_amount);

Coindesk::toFiatCurrency('USD', 1); // This will return 34381.5206 stating that ₿1 = $34,381.5206
Coindesk::toFiatCurrency('EUR', 1); // This will return 28431.5957 stating that ₿1 = €28,431.5957
Coindesk::toFiatCurrency('NGN', 1); // This will return 14261794.0186 stating that ₿1 = ₦14,261,794.0186
Coindesk::toFiatCurrency('USD', 1); // This will return 36579.71 stating that ₿1 = $36,579.71
Coindesk::toFiatCurrency('EUR', 1); // This will return 29951.01 stating that ₿1 = €29,951.01
Coindesk::toFiatCurrency('NGN', 1); // This will return 15028918.04 stating that ₿1 = ₦15,028,918.04
```

### To convert any Coindesk's supported fiat currency value to Bitcoin
Expand All @@ -55,9 +55,9 @@ use Coindesk;
*/
Coindesk::toBtc($amount, $currency_code);

Coindesk::toBtc(1, 'USD'); // This will return 2.8881896998808E-5
Coindesk::toBtc(1, 'EUR'); // This will return 3.5258494994032E-5
Coindesk::toBtc(1, 'NGN'); // This will return 7.0297483496097E-8
Coindesk::toBtc(1, 'USD'); // This will return 0.000027 stating that $1 = ₿0.000027
Coindesk::toBtc(1, 'EUR'); // This will return 0.000034 stating that €1 = ₿0.000034
Coindesk::toBtc(1, 'NGN'); // This will return 0.000000067 stating that ₦1 = ₿0.000000067
```

## Changelog
Expand Down
45 changes: 40 additions & 5 deletions src/Coindesk.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,11 @@ protected function parseToExchangeRatesArray($rawJsonData)
*
* @param string $currencyCode
* @param float $btcAmount
* @return float
* @return float|string
*
* @throws \GabrielAndy\Coindesk\Exceptions\CoindeskException
*/
public function toFiatCurrency($currencyCode, $btcAmount): float
public function toFiatCurrency($currencyCode, $btcAmount): float|string
{
if (! is_numeric($btcAmount)) {
throw new CoindeskException("
Expand All @@ -228,7 +228,7 @@ public function computeCurrencyValue($btcAmount, $rate)
{
$rate = is_numeric($rate) ? $rate : (float) $rate;

return $btcAmount * $rate;
return $this->formatInteger($btcAmount * $rate);
}

/**
Expand Down Expand Up @@ -256,14 +256,49 @@ public function toBtc($amount, $currencyCode): string
*
* @param float $amount
* @param float $rate
* @return float
* @return string
*
* @throws \GabrielAndy\Coindesk\Exceptions\CoindeskException
*/
public function computeBtcValue($amount, $rate)
{
$rate = is_numeric($rate) ? $rate : (float) $rate;

return $amount / $rate;
return $this->formatInteger($amount / $rate);
}

/**
* Format integer for monetal use while preserving insignificant values.
*
* @param float $integer
* @return string
*/
public function formatInteger(float $integer): string
{
// Get the float value of the integer
$amount = strval(floatval($integer));

// Split the amount to get the exponent
$amount = explode('E', $amount);

if (isset($amount[1])) {
$exponent = $amount[1];

$pos = strpos($exponent, '-', 0);

$exponent = abs($exponent);

if ($pos !== false) {
$digit = round($amount[0], 1);

$digit = bcdiv($digit, 10**$exponent, $exponent+1);
} else {
$digit = bcmul($amount[0], 10**$exponent, 2);
}
} else {
$digit = number_format($integer, 2, '.', '');
}

return $digit;
}
}

0 comments on commit 0bd71e2

Please sign in to comment.