-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Precision of floating point DynamoDB attributes #137
Comments
Thanks for reminding us that this needs to be documented. I don't think there is any way to workaround the ini limit enforced by ini_set('precision', 3);
var_export(json_encode(['foo' => 2.12345])); Outputs: So I think there are a two options:
|
Related and possibly of use as a solution! I've just done some quick messing around, and it does indeed look like printf() and sprintf() do not honour ini_get('precision'). Instead they seem to print the full accuracy of the float in question (ie. 6 digits for microtime(true)). Interesting... |
To clarify, sprintf() [and etc] seems to default to 6 decimal places but you can specify more - even more than ini_get('precision') |
I don't think there is much we can do in the SDK itself to convert floats to strings with the desired precision, but |
Hi Jeremy, So I can not use the sdk method "PutItem" like this way:
This seems doesn't working for the current SDK. Do we have to parse each node in the raw json and give something like 'S' => value instead? Thanks |
That will not work. The PHP SDK mirrors the actual API exactly, which requires the types be specified, even for values nested in M and L values. We are discussing ideas in #357 including one I have in this gist. If you have ffedback or ideas you'd like to share, please do so on #357. |
Thank you, that helps a lot. |
I feel it is important that both the devs and the userland folk are aware of a slight quirk with the DynamoDB Attribute class and factory() method.
Basically, due to line 100 of /src/Aws/DynamoDb/Model/Attribute.php [$attribute = new Attribute((string) $value, Type::NUMBER);] casting a float $value to a string, we have the potential to lose precision of the floating point value.
Because, when you cast a float to a string in PHP it will be a string with max precision of ini_get('precision').
For the time being I have had to hack around this by doing ini_set('precision', $someLargeNumber) before I call $ddbClient->formatAttributes() and what-have-you.
It would be super lovely if the SDK could automagically not lose any precision with the floating point attribute values.
Please see http://www.php.net/manual/en/ini.core.php#ini.precision and http://php.net/manual/en/language.types.float.php
Thanks a lot guys. This SDK is rockin' pretty hard!
The text was updated successfully, but these errors were encountered: