Remove floats.

This commit is contained in:
James Cole 2022-12-27 21:13:18 +01:00
parent 75ce777090
commit 5e654786be
No known key found for this signature in database
GPG Key ID: B49A324B7EAD6D80
19 changed files with 48 additions and 49 deletions

View File

@ -85,7 +85,7 @@ class UpdateRequest extends FormRequest
'notes', 'notes',
]; ];
$this->floatFields = [ $this->floatFields = [ // not really floats, for validation.
'amount', 'amount',
'foreign_amount', 'foreign_amount',
]; ];
@ -406,8 +406,7 @@ class UpdateRequest extends FormRequest
if (array_key_exists($fieldName, $transaction)) { if (array_key_exists($fieldName, $transaction)) {
$value = $transaction[$fieldName]; $value = $transaction[$fieldName];
if (is_float($value)) { if (is_float($value)) {
// TODO this effectively limits the max number of decimals in currencies to 14. $current[$fieldName] = sprintf('%.24f', $value);
$current[$fieldName] = sprintf('%.14f', $value);
} }
if (!is_float($value)) { if (!is_float($value)) {
$current[$fieldName] = (string) $value; $current[$fieldName] = (string) $value;

View File

@ -46,9 +46,8 @@ class PreferenceStoreRequest extends FormRequest
if ('false' === $array['data']) { if ('false' === $array['data']) {
$array['data'] = false; $array['data'] = false;
} }
// TODO remove float
if (is_numeric($array['data'])) { if (is_numeric($array['data'])) {
$array['data'] = (float) $array['data']; $array['data'] = (float) $array['data']; // intentional float.
} }
return $array; return $array;

View File

@ -47,9 +47,8 @@ class PreferenceUpdateRequest extends FormRequest
if ('false' === $array['data']) { if ('false' === $array['data']) {
$array['data'] = false; $array['data'] = false;
} }
// TODO remove float
if (is_numeric($array['data'])) { if (is_numeric($array['data'])) {
$array['data'] = (float) $array['data']; $array['data'] = (float) $array['data']; // intentional float.
} }
return $array; return $array;

View File

@ -542,7 +542,7 @@ class TagController extends Controller
$result[] = [ $result[] = [
'description' => $journal['description'], 'description' => $journal['description'],
'transaction_group_id' => $journal['transaction_group_id'], 'transaction_group_id' => $journal['transaction_group_id'],
'amount_float' => (float) $journal['amount'], 'amount_float' => (float) $journal['amount'], // intentional float.
'amount' => $journal['amount'], 'amount' => $journal['amount'],
'date' => $journal['date']->isoFormat($this->monthAndDayFormat), 'date' => $journal['date']->isoFormat($this->monthAndDayFormat),
'date_sort' => $journal['date']->format('Y-m-d'), 'date_sort' => $journal['date']->format('Y-m-d'),

View File

@ -99,7 +99,7 @@ class RecurrenceFormRequest extends FormRequest
]; ];
// fill in foreign currency data // fill in foreign currency data
if (null !== $this->convertFloat('foreign_amount')) { if (null !== $this->convertFloat('foreign_amount')) { // intentional float, used because it defaults to null.
$return['transactions'][0]['foreign_amount'] = $this->convertString('foreign_amount'); $return['transactions'][0]['foreign_amount'] = $this->convertString('foreign_amount');
$return['transactions'][0]['foreign_currency_id'] = $this->convertInteger('foreign_currency_id'); $return['transactions'][0]['foreign_currency_id'] = $this->convertInteger('foreign_currency_id');
} }
@ -228,7 +228,7 @@ class RecurrenceFormRequest extends FormRequest
$rules['repetitions'] = 'required|numeric|between:0,254'; $rules['repetitions'] = 'required|numeric|between:0,254';
} }
// if foreign amount, currency must be different. // if foreign amount, currency must be different.
if (null !== $this->convertFloat('foreign_amount')) { if (null !== $this->convertFloat('foreign_amount')) { // intentional float, used because it defaults to null.
$rules['foreign_currency_id'] = 'exists:transaction_currencies,id|different:transaction_currency_id'; $rules['foreign_currency_id'] = 'exists:transaction_currencies,id|different:transaction_currency_id';
} }

View File

@ -71,8 +71,8 @@ class Amount
*/ */
public function formatFlat(string $symbol, int $decimalPlaces, string $amount, bool $coloured = null): string public function formatFlat(string $symbol, int $decimalPlaces, string $amount, bool $coloured = null): string
{ {
$locale = app('steam')->getLocale(); $locale = app('steam')->getLocale();
$rounded = app('steam')->bcround($amount, $decimalPlaces); $rounded = app('steam')->bcround($amount, $decimalPlaces);
$coloured = $coloured ?? true; $coloured = $coloured ?? true;
$fmt = new NumberFormatter($locale, NumberFormatter::CURRENCY); $fmt = new NumberFormatter($locale, NumberFormatter::CURRENCY);

View File

@ -152,7 +152,7 @@ class FrontpageChartGenerator
$tempData[] = [ $tempData[] = [
'name' => trans('firefly.no_category'), 'name' => trans('firefly.no_category'),
'sum' => $currency['sum'], 'sum' => $currency['sum'],
'sum_float' => round((float) $currency['sum'], $currency['currency_decimal_places'] ?? 2), 'sum_float' => round((float) $currency['sum'], $currency['currency_decimal_places'] ?? 2), // intentional float
'currency_id' => (int) $currency['currency_id'], 'currency_id' => (int) $currency['currency_id'],
]; ];
} }

View File

@ -130,8 +130,8 @@ trait ModelInformation
$billTriggers = ['currency_is', 'amount_more', 'amount_less', 'description_contains']; $billTriggers = ['currency_is', 'amount_more', 'amount_less', 'description_contains'];
$values = [ $values = [
$bill->transactionCurrency()->first()->name, $bill->transactionCurrency()->first()->name,
round((float) $bill->amount_min, 24), $bill->amount_min,
round((float) $bill->amount_max, 24), $bill->amount_max,
$bill->name, $bill->name,
]; ];
foreach ($billTriggers as $index => $trigger) { foreach ($billTriggers as $index => $trigger) {

View File

@ -590,8 +590,10 @@ class Steam
if ($mantis < 0) { if ($mantis < 0) {
$post += abs((int)$mantis); $post += abs((int)$mantis);
} }
// TODO careless float could break financial math.
return number_format((float)$value, $post, '.', ''); return number_format((float)$value, $post, '.', '');
} }
// TODO careless float could break financial math.
return number_format((float)$value, 0, '.', ''); return number_format((float)$value, 0, '.', '');
} }

View File

@ -76,7 +76,7 @@ class AccountTransformer extends AbstractTransformer
[$openingBalance, $openingBalanceDate] = $this->getOpeningBalance($account, $accountType); [$openingBalance, $openingBalanceDate] = $this->getOpeningBalance($account, $accountType);
[$interest, $interestPeriod] = $this->getInterest($account, $accountType); [$interest, $interestPeriod] = $this->getInterest($account, $accountType);
$openingBalance = number_format((float) $openingBalance, $decimalPlaces, '.', ''); $openingBalance = app('steam')->bcround($openingBalance, $decimalPlaces);
$includeNetWorth = '0' !== $this->repository->getMetaValue($account, 'include_net_worth'); $includeNetWorth = '0' !== $this->repository->getMetaValue($account, 'include_net_worth');
$longitude = null; $longitude = null;
$latitude = null; $latitude = null;
@ -107,7 +107,7 @@ class AccountTransformer extends AbstractTransformer
'currency_code' => $currencyCode, 'currency_code' => $currencyCode,
'currency_symbol' => $currencySymbol, 'currency_symbol' => $currencySymbol,
'currency_decimal_places' => $decimalPlaces, 'currency_decimal_places' => $decimalPlaces,
'current_balance' => number_format((float) app('steam')->balance($account, $date), $decimalPlaces, '.', ''), 'current_balance' => app('steam')->bcround(app('steam')->balance($account, $date), $decimalPlaces),
'current_balance_date' => $date->toAtomString(), 'current_balance_date' => $date->toAtomString(),
'notes' => $this->repository->getNoteText($account), 'notes' => $this->repository->getNoteText($account),
'monthly_payment_date' => $monthlyPaymentDate, 'monthly_payment_date' => $monthlyPaymentDate,
@ -115,7 +115,7 @@ class AccountTransformer extends AbstractTransformer
'account_number' => $this->repository->getMetaValue($account, 'account_number'), 'account_number' => $this->repository->getMetaValue($account, 'account_number'),
'iban' => '' === $account->iban ? null : $account->iban, 'iban' => '' === $account->iban ? null : $account->iban,
'bic' => $this->repository->getMetaValue($account, 'BIC'), 'bic' => $this->repository->getMetaValue($account, 'BIC'),
'virtual_balance' => number_format((float) $account->virtual_balance, $decimalPlaces, '.', ''), 'virtual_balance' => app('steam')->bcround($account->virtual_balance, $decimalPlaces),
'opening_balance' => $openingBalance, 'opening_balance' => $openingBalance,
'opening_balance_date' => $openingBalanceDate, 'opening_balance_date' => $openingBalanceDate,
'liability_type' => $liabilityType, 'liability_type' => $liabilityType,

View File

@ -69,7 +69,7 @@ class AvailableBudgetTransformer extends AbstractTransformer
'currency_code' => $currency->code, 'currency_code' => $currency->code,
'currency_symbol' => $currency->symbol, 'currency_symbol' => $currency->symbol,
'currency_decimal_places' => (int) $currency->decimal_places, 'currency_decimal_places' => (int) $currency->decimal_places,
'amount' => number_format((float) $availableBudget->amount, $currency->decimal_places, '.', ''), 'amount' => app('steam')->bcround($availableBudget->amount, $currency->decimal_places),
'start' => $availableBudget->start_date->toAtomString(), 'start' => $availableBudget->start_date->toAtomString(),
'end' => $availableBudget->end_date->endOfDay()->toAtomString(), 'end' => $availableBudget->end_date->endOfDay()->toAtomString(),
'spent_in_budgets' => [], 'spent_in_budgets' => [],

View File

@ -115,8 +115,8 @@ class BillTransformer extends AbstractTransformer
'currency_symbol' => $currency->symbol, 'currency_symbol' => $currency->symbol,
'currency_decimal_places' => (int) $currency->decimal_places, 'currency_decimal_places' => (int) $currency->decimal_places,
'name' => $bill->name, 'name' => $bill->name,
'amount_min' => number_format((float) $bill->amount_min, $currency->decimal_places, '.', ''), 'amount_min' => app('steam')->bcround($bill->amount_min, $currency->decimal_places),
'amount_max' => number_format((float) $bill->amount_max, $currency->decimal_places, '.', ''), 'amount_max' => app('steam')->bcround($bill->amount_max, $currency->decimal_places),
'date' => $bill->date->toAtomString(), 'date' => $bill->date->toAtomString(),
'end_date' => $bill->end_date?->toAtomString(), 'end_date' => $bill->end_date?->toAtomString(),
'extension_date' => $bill->extension_date?->toAtomString(), 'extension_date' => $bill->extension_date?->toAtomString(),

View File

@ -80,7 +80,7 @@ class BudgetLimitTransformer extends AbstractTransformer
$currencySymbol = $currency->symbol; $currencySymbol = $currency->symbol;
$currencyDecimalPlaces = $currency->decimal_places; $currencyDecimalPlaces = $currency->decimal_places;
} }
$amount = number_format((float) $amount, $currencyDecimalPlaces, '.', ''); $amount = app('steam')->bcround($amount, $currencyDecimalPlaces);
return [ return [
'id' => (string) $budgetLimit->id, 'id' => (string) $budgetLimit->id,

View File

@ -84,7 +84,7 @@ class BudgetTransformer extends AbstractTransformer
$abCurrencyId = (string) $autoBudget->transactionCurrency->id; $abCurrencyId = (string) $autoBudget->transactionCurrency->id;
$abCurrencyCode = $autoBudget->transactionCurrency->code; $abCurrencyCode = $autoBudget->transactionCurrency->code;
$abType = $types[$autoBudget->auto_budget_type]; $abType = $types[$autoBudget->auto_budget_type];
$abAmount = number_format((float) $autoBudget->amount, $autoBudget->transactionCurrency->decimal_places, '.', ''); $abAmount = app('steam')->bcround($autoBudget->amount, $autoBudget->transactionCurrency->decimal_places);
$abPeriod = $autoBudget->period; $abPeriod = $autoBudget->period;
} }
@ -120,7 +120,7 @@ class BudgetTransformer extends AbstractTransformer
{ {
$return = []; $return = [];
foreach ($array as $data) { foreach ($array as $data) {
$data['sum'] = number_format((float) $data['sum'], (int) $data['currency_decimal_places'], '.', ''); $data['sum'] = app('steam')->bcround($data['sum'], (int) $data['currency_decimal_places']);
$return[] = $data; $return[] = $data;
} }

View File

@ -95,7 +95,7 @@ class CategoryTransformer extends AbstractTransformer
{ {
$return = []; $return = [];
foreach ($array as $data) { foreach ($array as $data) {
$data['sum'] = number_format((float) $data['sum'], (int) $data['currency_decimal_places'], '.', ''); $data['sum'] = app('steam')->bcround($data['sum'], (int) $data['currency_decimal_places']);
$return[] = $data; $return[] = $data;
} }

View File

@ -83,7 +83,7 @@ class PiggyBankEventTransformer extends AbstractTransformer
'id' => (string) $event->id, 'id' => (string) $event->id,
'created_at' => $event->created_at->toAtomString(), 'created_at' => $event->created_at->toAtomString(),
'updated_at' => $event->updated_at->toAtomString(), 'updated_at' => $event->updated_at->toAtomString(),
'amount' => number_format((float) $event->amount, $currency->decimal_places, '.', ''), 'amount' => app('steam')->bcround($event->amount, $currency->decimal_places),
'currency_id' => (string) $currency->id, 'currency_id' => (string) $currency->id,
'currency_code' => $currency->code, 'currency_code' => $currency->code,
'currency_symbol' => $currency->symbol, 'currency_symbol' => $currency->symbol,

View File

@ -53,7 +53,7 @@ class PiggyBankTransformer extends AbstractTransformer
/** /**
* Transform the piggy bank. * Transform the piggy bank.
* *
* @param PiggyBank $piggyBank * @param PiggyBank $piggyBank
* *
* @return array * @return array
* @throws \FireflyIII\Exceptions\FireflyException * @throws \FireflyIII\Exceptions\FireflyException
@ -81,44 +81,44 @@ class PiggyBankTransformer extends AbstractTransformer
/** @var ObjectGroup $objectGroup */ /** @var ObjectGroup $objectGroup */
$objectGroup = $piggyBank->objectGroups->first(); $objectGroup = $piggyBank->objectGroups->first();
if (null !== $objectGroup) { if (null !== $objectGroup) {
$objectGroupId = (int) $objectGroup->id; $objectGroupId = (int)$objectGroup->id;
$objectGroupOrder = (int) $objectGroup->order; $objectGroupOrder = (int)$objectGroup->order;
$objectGroupTitle = $objectGroup->title; $objectGroupTitle = $objectGroup->title;
} }
// get currently saved amount: // get currently saved amount:
$currentAmountStr = $this->piggyRepos->getCurrentAmount($piggyBank); $currentAmountStr = $this->piggyRepos->getCurrentAmount($piggyBank);
$currentAmount = number_format((float) $currentAmountStr, $currency->decimal_places, '.', ''); $currentAmount = app('steam')->bcround($currentAmountStr, $currency->decimal_places);
// Amounts, depending on 0.0 state of target amount // Amounts, depending on 0.0 state of target amount
$percentage = null; $percentage = null;
$targetAmountString = null; $targetAmountString = null;
$leftToSaveString = null; $leftToSaveString = null;
$savePerMonth = null; $savePerMonth = null;
if (0.000 !== (float) $piggyBank->targetamount) { if (0 !== bccomp(app('steam')->bcround($currentAmountStr, $currency->decimal_places), '0')) {
$leftToSave = bcsub($piggyBank->targetamount, $currentAmountStr); $leftToSave = bcsub($piggyBank->targetamount, $currentAmountStr);
$targetAmount = (string) $piggyBank->targetamount; $targetAmount = (string)$piggyBank->targetamount;
$targetAmount = 1 === bccomp('0.01', $targetAmount) ? '0.01' : $targetAmount; $targetAmount = 1 === bccomp('0.01', $targetAmount) ? '0.01' : $targetAmount;
$percentage = (int) (0 !== bccomp('0', $currentAmountStr) ? $currentAmountStr / $targetAmount * 100 : 0); $percentage = (int)(0 !== bccomp('0', $currentAmountStr) ? $currentAmountStr / $targetAmount * 100 : 0);
$targetAmountString = number_format((float) $targetAmount, $currency->decimal_places, '.', ''); $targetAmountString = app('steam')->bcround($targetAmount, $currency->decimal_places);
$leftToSaveString = number_format((float) $leftToSave, $currency->decimal_places, '.', ''); $leftToSaveString = app('steam')->bcround($leftToSave, $currency->decimal_places);
$savePerMonth = number_format((float) $this->piggyRepos->getSuggestedMonthlyAmount($piggyBank), $currency->decimal_places, '.', ''); $savePerMonth = app('steam')->bcround($this->piggyRepos->getSuggestedMonthlyAmount($piggyBank), $currency->decimal_places);
} }
$startDate = $piggyBank->startdate?->toAtomString(); $startDate = $piggyBank->startdate?->toAtomString();
$targetDate = $piggyBank->targetdate?->toAtomString(); $targetDate = $piggyBank->targetdate?->toAtomString();
return [ return [
'id' => (string) $piggyBank->id, 'id' => (string)$piggyBank->id,
'created_at' => $piggyBank->created_at->toAtomString(), 'created_at' => $piggyBank->created_at->toAtomString(),
'updated_at' => $piggyBank->updated_at->toAtomString(), 'updated_at' => $piggyBank->updated_at->toAtomString(),
'account_id' => (string) $piggyBank->account_id, 'account_id' => (string)$piggyBank->account_id,
'account_name' => $piggyBank->account->name, 'account_name' => $piggyBank->account->name,
'name' => $piggyBank->name, 'name' => $piggyBank->name,
'currency_id' => (string) $currency->id, 'currency_id' => (string)$currency->id,
'currency_code' => $currency->code, 'currency_code' => $currency->code,
'currency_symbol' => $currency->symbol, 'currency_symbol' => $currency->symbol,
'currency_decimal_places' => (int) $currency->decimal_places, 'currency_decimal_places' => (int)$currency->decimal_places,
'target_amount' => $targetAmountString, 'target_amount' => $targetAmountString,
'percentage' => $percentage, 'percentage' => $percentage,
'current_amount' => $currentAmount, 'current_amount' => $currentAmount,
@ -126,16 +126,16 @@ class PiggyBankTransformer extends AbstractTransformer
'save_per_month' => $savePerMonth, 'save_per_month' => $savePerMonth,
'start_date' => $startDate, 'start_date' => $startDate,
'target_date' => $targetDate, 'target_date' => $targetDate,
'order' => (int) $piggyBank->order, 'order' => (int)$piggyBank->order,
'active' => true, 'active' => true,
'notes' => $notes, 'notes' => $notes,
'object_group_id' => $objectGroupId ? (string) $objectGroupId : null, 'object_group_id' => $objectGroupId ? (string)$objectGroupId : null,
'object_group_order' => $objectGroupOrder, 'object_group_order' => $objectGroupOrder,
'object_group_title' => $objectGroupTitle, 'object_group_title' => $objectGroupTitle,
'links' => [ 'links' => [
[ [
'rel' => 'self', 'rel' => 'self',
'uri' => '/piggy_banks/' . $piggyBank->id, 'uri' => '/piggy_banks/'.$piggyBank->id,
], ],
], ],
]; ];

View File

@ -198,10 +198,10 @@ class RecurrenceTransformer extends AbstractTransformer
$destinationType = $destinationAccount->accountType->type; $destinationType = $destinationAccount->accountType->type;
$destinationIban = $destinationAccount->iban; $destinationIban = $destinationAccount->iban;
} }
$amount = number_format((float) $transaction->amount, $transaction->transactionCurrency->decimal_places, '.', ''); $amount = app('steam')->bcround($transaction->amount, $transaction->transactionCurrency->decimal_places);
$foreignAmount = null; $foreignAmount = null;
if (null !== $transaction->foreign_currency_id && null !== $transaction->foreign_amount) { if (null !== $transaction->foreign_currency_id && null !== $transaction->foreign_amount) {
$foreignAmount = number_format((float) $transaction->foreign_amount, $foreignCurrencyDp, '.', ''); $foreignAmount = app('steam')->bcround($transaction->foreign_amount, $foreignCurrencyDp);
} }
$transactionArray = [ $transactionArray = [
'currency_id' => (string) $transaction->transaction_currency_id, 'currency_id' => (string) $transaction->transaction_currency_id,

View File

@ -363,7 +363,7 @@ class TransactionGroupTransformer extends AbstractTransformer
$bill = $this->getBill($journal->bill); $bill = $this->getBill($journal->bill);
if (null !== $foreignAmount && null !== $foreignCurrency) { if (null !== $foreignAmount && null !== $foreignCurrency) {
$foreignAmount = number_format((float) $foreignAmount, $foreignCurrency['decimal_places'] ?? 0, '.', ''); $foreignAmount = app('steam')->bcround($foreignAmount, $foreignCurrency['decimal_places'] ?? 0);
} }
$longitude = null; $longitude = null;
@ -393,7 +393,7 @@ class TransactionGroupTransformer extends AbstractTransformer
'foreign_currency_symbol' => $foreignCurrency['symbol'], 'foreign_currency_symbol' => $foreignCurrency['symbol'],
'foreign_currency_decimal_places' => $foreignCurrency['decimal_places'], 'foreign_currency_decimal_places' => $foreignCurrency['decimal_places'],
'amount' => number_format((float) $amount, $currency->decimal_places, '.', ''), 'amount' => app('steam')->bcround($amount, $currency->decimal_places),
'foreign_amount' => $foreignAmount, 'foreign_amount' => $foreignAmount,
'description' => $journal->description, 'description' => $journal->description,
@ -461,7 +461,7 @@ class TransactionGroupTransformer extends AbstractTransformer
{ {
$result = $journal->transactions->first( $result = $journal->transactions->first(
static function (Transaction $transaction) { static function (Transaction $transaction) {
return (float) $transaction->amount < 0; return (float) $transaction->amount < 0; // lame but it works.
} }
); );
if (null === $result) { if (null === $result) {
@ -481,7 +481,7 @@ class TransactionGroupTransformer extends AbstractTransformer
{ {
$result = $journal->transactions->first( $result = $journal->transactions->first(
static function (Transaction $transaction) { static function (Transaction $transaction) {
return (float) $transaction->amount > 0; return (float) $transaction->amount > 0; // lame but it works
} }
); );
if (null === $result) { if (null === $result) {