From e472c3520d1bbd9df5b6a81c484b1990725b7ba6 Mon Sep 17 00:00:00 2001 From: James Cole Date: Sun, 16 Oct 2022 14:44:11 +0200 Subject: [PATCH] Add comments about floats --- app/Api/V1/Controllers/Chart/AccountController.php | 5 +++-- .../Insight/Expense/AccountController.php | 4 ++-- .../Controllers/Insight/Expense/BillController.php | 8 ++++---- .../Controllers/Insight/Expense/BudgetController.php | 4 ++-- .../Insight/Expense/CategoryController.php | 4 ++-- .../Controllers/Insight/Expense/PeriodController.php | 4 ++-- .../V1/Controllers/Insight/Expense/TagController.php | 8 ++++---- .../Controllers/Insight/Income/AccountController.php | 4 ++-- .../Insight/Income/CategoryController.php | 4 ++-- .../Controllers/Insight/Income/PeriodController.php | 4 ++-- app/Api/V2/Controllers/Chart/AccountController.php | 7 +++++-- app/Helpers/Report/NetWorth.php | 12 ++++++------ 12 files changed, 36 insertions(+), 32 deletions(-) diff --git a/app/Api/V1/Controllers/Chart/AccountController.php b/app/Api/V1/Controllers/Chart/AccountController.php index 264c1a97a0..b6f0d937ed 100644 --- a/app/Api/V1/Controllers/Chart/AccountController.php +++ b/app/Api/V1/Controllers/Chart/AccountController.php @@ -127,11 +127,12 @@ class AccountController extends Controller ]; $currentStart = clone $start; $range = app('steam')->balanceInRange($account, $start, clone $end); - $previous = round((float) array_values($range)[0], 12); + // 2022-10-11 this method no longer converts to float. + $previous = array_values($range)[0]; while ($currentStart <= $end) { $format = $currentStart->format('Y-m-d'); $label = $currentStart->toAtomString(); - $balance = array_key_exists($format, $range) ? round((float) $range[$format], 12) : $previous; + $balance = array_key_exists($format, $range) ? $range[$format] : $previous; $previous = $balance; $currentStart->addDay(); $currentSet['entries'][$label] = $balance; diff --git a/app/Api/V1/Controllers/Insight/Expense/AccountController.php b/app/Api/V1/Controllers/Insight/Expense/AccountController.php index cb2f74ca68..51dcd4b84c 100644 --- a/app/Api/V1/Controllers/Insight/Expense/AccountController.php +++ b/app/Api/V1/Controllers/Insight/Expense/AccountController.php @@ -94,7 +94,7 @@ class AccountController extends Controller 'id' => (string) $expense['id'], 'name' => $expense['name'], 'difference' => $expense['sum'], - 'difference_float' => (float) $expense['sum'], + 'difference_float' => (float) $expense['sum'], // float but on purpose. 'currency_id' => (string) $expense['currency_id'], 'currency_code' => $expense['currency_code'], ]; @@ -126,7 +126,7 @@ class AccountController extends Controller 'id' => (string) $expense['id'], 'name' => $expense['name'], 'difference' => $expense['sum'], - 'difference_float' => (float) $expense['sum'], + 'difference_float' => (float) $expense['sum'], // float but on purpose. 'currency_id' => (string) $expense['currency_id'], 'currency_code' => $expense['currency_code'], ]; diff --git a/app/Api/V1/Controllers/Insight/Expense/BillController.php b/app/Api/V1/Controllers/Insight/Expense/BillController.php index 4bcf455c23..f43f33d389 100644 --- a/app/Api/V1/Controllers/Insight/Expense/BillController.php +++ b/app/Api/V1/Controllers/Insight/Expense/BillController.php @@ -101,7 +101,7 @@ class BillController extends Controller 'currency_code' => $journal['currency_code'], ]; $response[$key]['difference'] = bcadd($response[$key]['difference'], $journal['amount']); - $response[$key]['difference_float'] = (float) $response[$key]['difference']; + $response[$key]['difference_float'] = (float) $response[$key]['difference']; // float but on purpose. } if (0 !== $foreignCurrencyId) { $response[$foreignKey] = $response[$foreignKey] ?? [ @@ -111,7 +111,7 @@ class BillController extends Controller 'currency_code' => $journal['foreign_currency_code'], ]; $response[$foreignKey]['difference'] = bcadd($response[$foreignKey]['difference'], $journal['foreign_amount']); - $response[$foreignKey]['difference_float'] = (float) $response[$foreignKey]['difference']; + $response[$foreignKey]['difference_float'] = (float) $response[$foreignKey]['difference']; // float but on purpose. } } @@ -154,7 +154,7 @@ class BillController extends Controller 'currency_code' => $journal['currency_code'], ]; $response[$currencyId]['difference'] = bcadd($response[$currencyId]['difference'], $journal['amount']); - $response[$currencyId]['difference_float'] = (float) $response[$currencyId]['difference']; + $response[$currencyId]['difference_float'] = (float) $response[$currencyId]['difference']; // float but on purpose. } if (0 !== $foreignCurrencyId) { $response[$foreignCurrencyId] = $response[$foreignCurrencyId] ?? [ @@ -164,7 +164,7 @@ class BillController extends Controller 'currency_code' => $journal['foreign_currency_code'], ]; $response[$foreignCurrencyId]['difference'] = bcadd($response[$foreignCurrencyId]['difference'], $journal['foreign_amount']); - $response[$foreignCurrencyId]['difference_float'] = (float) $response[$foreignCurrencyId]['difference']; + $response[$foreignCurrencyId]['difference_float'] = (float) $response[$foreignCurrencyId]['difference']; // float but on purpose. } } diff --git a/app/Api/V1/Controllers/Insight/Expense/BudgetController.php b/app/Api/V1/Controllers/Insight/Expense/BudgetController.php index cf135f6abd..d0b3442d87 100644 --- a/app/Api/V1/Controllers/Insight/Expense/BudgetController.php +++ b/app/Api/V1/Controllers/Insight/Expense/BudgetController.php @@ -91,7 +91,7 @@ class BudgetController extends Controller 'id' => (string) $budget->id, 'name' => $budget->name, 'difference' => $expense['sum'], - 'difference_float' => (float) $expense['sum'], + 'difference_float' => (float) $expense['sum'], // float but on purpose. 'currency_id' => (string) $expense['currency_id'], 'currency_code' => $expense['currency_code'], ]; @@ -120,7 +120,7 @@ class BudgetController extends Controller foreach ($expenses as $expense) { $result[] = [ 'difference' => $expense['sum'], - 'difference_float' => (float) $expense['sum'], + 'difference_float' => (float) $expense['sum'], // float but on purpose. 'currency_id' => (string) $expense['currency_id'], 'currency_code' => $expense['currency_code'], ]; diff --git a/app/Api/V1/Controllers/Insight/Expense/CategoryController.php b/app/Api/V1/Controllers/Insight/Expense/CategoryController.php index cc2b25fc4a..b9745ccc62 100644 --- a/app/Api/V1/Controllers/Insight/Expense/CategoryController.php +++ b/app/Api/V1/Controllers/Insight/Expense/CategoryController.php @@ -92,7 +92,7 @@ class CategoryController extends Controller 'id' => (string) $category->id, 'name' => $category->name, 'difference' => $expense['sum'], - 'difference_float' => (float) $expense['sum'], + 'difference_float' => (float) $expense['sum'], // float but on purpose. 'currency_id' => (string) $expense['currency_id'], 'currency_code' => $expense['currency_code'], ]; @@ -121,7 +121,7 @@ class CategoryController extends Controller foreach ($expenses as $expense) { $result[] = [ 'difference' => $expense['sum'], - 'difference_float' => (float) $expense['sum'], + 'difference_float' => (float) $expense['sum'], // float but on purpose. 'currency_id' => (string) $expense['currency_id'], 'currency_code' => $expense['currency_code'], ]; diff --git a/app/Api/V1/Controllers/Insight/Expense/PeriodController.php b/app/Api/V1/Controllers/Insight/Expense/PeriodController.php index 6d32267b8f..adca07dfcb 100644 --- a/app/Api/V1/Controllers/Insight/Expense/PeriodController.php +++ b/app/Api/V1/Controllers/Insight/Expense/PeriodController.php @@ -66,7 +66,7 @@ class PeriodController extends Controller 'currency_code' => $journal['currency_code'], ]; $response[$currencyId]['difference'] = bcadd($response[$currencyId]['difference'], $journal['amount']); - $response[$currencyId]['difference_float'] = (float) $response[$currencyId]['difference']; + $response[$currencyId]['difference_float'] = (float) $response[$currencyId]['difference']; // float but on purpose. } if (0 !== $foreignCurrencyId) { $response[$foreignCurrencyId] = $response[$foreignCurrencyId] ?? [ @@ -76,7 +76,7 @@ class PeriodController extends Controller 'currency_code' => $journal['foreign_currency_code'], ]; $response[$foreignCurrencyId]['difference'] = bcadd($response[$foreignCurrencyId]['difference'], $journal['foreign_amount']); - $response[$foreignCurrencyId]['difference_float'] = (float) $response[$foreignCurrencyId]['difference']; + $response[$foreignCurrencyId]['difference_float'] = (float) $response[$foreignCurrencyId]['difference']; // float but on purpose. } } diff --git a/app/Api/V1/Controllers/Insight/Expense/TagController.php b/app/Api/V1/Controllers/Insight/Expense/TagController.php index 9ea0a0fa04..48e8a0e04c 100644 --- a/app/Api/V1/Controllers/Insight/Expense/TagController.php +++ b/app/Api/V1/Controllers/Insight/Expense/TagController.php @@ -90,7 +90,7 @@ class TagController extends Controller 'currency_code' => $journal['currency_code'], ]; $response[$currencyId]['difference'] = bcadd($response[$currencyId]['difference'], $journal['amount']); - $response[$currencyId]['difference_float'] = (float) $response[$currencyId]['difference']; + $response[$currencyId]['difference_float'] = (float) $response[$currencyId]['difference']; // float but on purpose. } if (0 !== $foreignCurrencyId) { $response[$foreignCurrencyId] = $response[$foreignCurrencyId] ?? [ @@ -100,7 +100,7 @@ class TagController extends Controller 'currency_code' => $journal['foreign_currency_code'], ]; $response[$foreignCurrencyId]['difference'] = bcadd($response[$foreignCurrencyId]['difference'], $journal['foreign_amount']); - $response[$foreignCurrencyId]['difference_float'] = (float) $response[$foreignCurrencyId]['difference']; + $response[$foreignCurrencyId]['difference_float'] = (float) $response[$foreignCurrencyId]['difference']; // float but on purpose. } } @@ -157,7 +157,7 @@ class TagController extends Controller 'currency_code' => $journal['currency_code'], ]; $response[$key]['difference'] = bcadd($response[$key]['difference'], $journal['amount']); - $response[$key]['difference_float'] = (float) $response[$key]['difference']; + $response[$key]['difference_float'] = (float) $response[$key]['difference']; // float but on purpose. } // on foreign ID @@ -169,7 +169,7 @@ class TagController extends Controller 'currency_code' => $journal['foreign_currency_code'], ]; $response[$foreignKey]['difference'] = bcadd($response[$foreignKey]['difference'], $journal['foreign_amount']); - $response[$foreignKey]['difference_float'] = (float) $response[$foreignKey]['difference']; + $response[$foreignKey]['difference_float'] = (float) $response[$foreignKey]['difference']; // float but on purpose. } } } diff --git a/app/Api/V1/Controllers/Insight/Income/AccountController.php b/app/Api/V1/Controllers/Insight/Income/AccountController.php index ecad08b6b4..09ed57e515 100644 --- a/app/Api/V1/Controllers/Insight/Income/AccountController.php +++ b/app/Api/V1/Controllers/Insight/Income/AccountController.php @@ -93,7 +93,7 @@ class AccountController extends Controller 'id' => (string) $entry['id'], 'name' => $entry['name'], 'difference' => $entry['sum'], - 'difference_float' => (float) $entry['sum'], + 'difference_float' => (float) $entry['sum'], // float but on purpose. 'currency_id' => (string) $entry['currency_id'], 'currency_code' => $entry['currency_code'], ]; @@ -125,7 +125,7 @@ class AccountController extends Controller 'id' => (string) $entry['id'], 'name' => $entry['name'], 'difference' => $entry['sum'], - 'difference_float' => (float) $entry['sum'], + 'difference_float' => (float) $entry['sum'], // float but on purpose. 'currency_id' => (string) $entry['currency_id'], 'currency_code' => $entry['currency_code'], ]; diff --git a/app/Api/V1/Controllers/Insight/Income/CategoryController.php b/app/Api/V1/Controllers/Insight/Income/CategoryController.php index 79f0c4bf79..fad9df43b3 100644 --- a/app/Api/V1/Controllers/Insight/Income/CategoryController.php +++ b/app/Api/V1/Controllers/Insight/Income/CategoryController.php @@ -92,7 +92,7 @@ class CategoryController extends Controller 'id' => (string) $category->id, 'name' => $category->name, 'difference' => $expense['sum'], - 'difference_float' => (float) $expense['sum'], + 'difference_float' => (float) $expense['sum'], // float but on purpose. 'currency_id' => (string) $expense['currency_id'], 'currency_code' => $expense['currency_code'], ]; @@ -121,7 +121,7 @@ class CategoryController extends Controller foreach ($expenses as $expense) { $result[] = [ 'difference' => $expense['sum'], - 'difference_float' => (float) $expense['sum'], + 'difference_float' => (float) $expense['sum'], // float but on purpose. 'currency_id' => (string) $expense['currency_id'], 'currency_code' => $expense['currency_code'], ]; diff --git a/app/Api/V1/Controllers/Insight/Income/PeriodController.php b/app/Api/V1/Controllers/Insight/Income/PeriodController.php index 41aa682337..a7245d2597 100644 --- a/app/Api/V1/Controllers/Insight/Income/PeriodController.php +++ b/app/Api/V1/Controllers/Insight/Income/PeriodController.php @@ -66,7 +66,7 @@ class PeriodController extends Controller 'currency_code' => $journal['currency_code'], ]; $response[$currencyId]['difference'] = bcadd($response[$currencyId]['difference'], app('steam')->positive($journal['amount'])); - $response[$currencyId]['difference_float'] = (float) $response[$currencyId]['difference']; + $response[$currencyId]['difference_float'] = (float) $response[$currencyId]['difference']; // float but on purpose. } if (0 !== $foreignCurrencyId) { $response[$foreignCurrencyId] = $response[$foreignCurrencyId] ?? [ @@ -78,7 +78,7 @@ class PeriodController extends Controller $response[$foreignCurrencyId]['difference'] = bcadd( $response[$foreignCurrencyId]['difference'], app('steam')->positive($journal['foreign_amount']) ); - $response[$foreignCurrencyId]['difference_float'] = (float) $response[$foreignCurrencyId]['difference']; + $response[$foreignCurrencyId]['difference_float'] = (float) $response[$foreignCurrencyId]['difference']; // float but on purpose. } } diff --git a/app/Api/V2/Controllers/Chart/AccountController.php b/app/Api/V2/Controllers/Chart/AccountController.php index e12df6dce2..c611ae2e59 100644 --- a/app/Api/V2/Controllers/Chart/AccountController.php +++ b/app/Api/V2/Controllers/Chart/AccountController.php @@ -102,11 +102,14 @@ class AccountController extends Controller ]; $currentStart = clone $start; $range = app('steam')->balanceInRange($account, $start, clone $end); - $previous = round((float) array_values($range)[0], 12); + + // 2022-10-11: this method no longer converts to floats + + $previous = array_values($range)[0]; while ($currentStart <= $end) { $format = $currentStart->format('Y-m-d'); $label = $currentStart->toAtomString(); - $balance = array_key_exists($format, $range) ? round((float) $range[$format], 12) : $previous; + $balance = array_key_exists($format, $range) ? $range[$format] : $previous; $previous = $balance; $currentStart->addDay(); $currentSet['entries'][$label] = $balance; diff --git a/app/Helpers/Report/NetWorth.php b/app/Helpers/Report/NetWorth.php index ec2a434f13..6f816018e7 100644 --- a/app/Helpers/Report/NetWorth.php +++ b/app/Helpers/Report/NetWorth.php @@ -76,7 +76,7 @@ class NetWorth implements NetWorthInterface $netWorth = []; $result = []; - //Log::debug(sprintf('Now in getNetWorthByCurrency(%s)', $date->format('Y-m-d'))); + Log::debug(sprintf('Now in getNetWorthByCurrency(%s)', $date->format('Y-m-d'))); // get default currency $default = app('amount')->getDefaultCurrencyByUser($this->user); @@ -87,16 +87,16 @@ class NetWorth implements NetWorthInterface // get the preferred currency for this account /** @var Account $account */ foreach ($accounts as $account) { - //Log::debug(sprintf('Now at account #%d: "%s"', $account->id, $account->name)); + Log::debug(sprintf('Now at account #%d: "%s"', $account->id, $account->name)); $currencyId = (int) $this->accountRepository->getMetaValue($account, 'currency_id'); $currencyId = 0 === $currencyId ? $default->id : $currencyId; - //Log::debug(sprintf('Currency ID is #%d', $currencyId)); + Log::debug(sprintf('Currency ID is #%d', $currencyId)); // balance in array: $balance = $balances[$account->id] ?? '0'; - //Log::debug(sprintf('Balance is %s', $balance)); + Log::debug(sprintf('Balance for %s is %s', $date->format('Y-m-d'), $balance)); // always subtract virtual balance. $virtualBalance = (string) $account->virtual_balance; @@ -104,14 +104,14 @@ class NetWorth implements NetWorthInterface $balance = bcsub($balance, $virtualBalance); } - //Log::debug(sprintf('Balance corrected to %s because of virtual balance (%s)', $balance, $virtualBalance)); + Log::debug(sprintf('Balance corrected to %s because of virtual balance (%s)', $balance, $virtualBalance)); if (!array_key_exists($currencyId, $netWorth)) { $netWorth[$currencyId] = '0'; } $netWorth[$currencyId] = bcadd($balance, $netWorth[$currencyId]); - //Log::debug(sprintf('Total net worth for currency #%d is %s', $currencyId, $netWorth[$currencyId])); + Log::debug(sprintf('Total net worth for currency #%d is %s', $currencyId, $netWorth[$currencyId])); } ksort($netWorth);