link0 / bunq

API client in PHP for bunq
MIT License
8 stars 13 forks source link

balance is shown as string in object instead of float #22

Closed jerry89899 closed 7 years ago

jerry89899 commented 7 years ago
screen shot 2017-05-16 at 18 45 01

Here it shows that the amount of money i have is 29164 which i sadly dont have i only have 291,64. is there an way to let it show an float?

$monetaryAccountService` = new MonetaryAccountService($client);
$monetaryAccounts = $monetaryAccountService->listByUser($user);
echo json_encode($monetaryAccounts[0]->balance());

Here is the noob code i used. haha

verschoof commented 7 years ago

The result you got are euro cents. (we don't really like floats :) )

so this will fix your problem:

$amount = 29164;

$euro = $amount / 100;

As far I can see the code they use now the Money object. https://github.com/moneyphp/money http://sandbox.onlinephpfunctions.com/code/190fa1893b934f6cd0e25072ea520d662b078660

dennisdegreef commented 7 years ago

Indeed the return value of balance() is a Money object, which in serialized form looks like the JSON you pasted. With floating point values, you lose precision, while the Money object is designed to avoid just that. Indeed you can devide by 100 and cast to a float if you like, but since you can chain calls and do calculations with them, I prefer to use the Money object for this.

But thanks for the feedback here :) is your problem solved now?

dennisdegreef commented 7 years ago

I have closed the issue, if you have any questions further, feel free to open a new issue. Thanks for your question!