pronamic / wp-pronamic-pay

The Pronamic Pay plugin allows you to easily accept payments with payment methods like credit card, iDEAL, Bancontact and Sofort through a variety of payment providers on your WordPress website.
https://pronamicpay.com
34 stars 14 forks source link

Gravity Forms → Product price `29.95` `=` `29.949999999999999` #281

Closed remcotolsma closed 2 years ago

remcotolsma commented 2 years ago

https://secure.helpscout.net/conversation/1696891636/23013?folderId=1425713

I can reproduce this issue on a Local WordPress installation.

After some debugging i think this has something to do with serialize_precision configuration option.

I couldn't reproduce this setting via PHPUnit or via https://3v4l.org/, it seems that serialize_precision is set to -1 in these environments:

echo ini_get( 'serialize_precision' );
php -r "echo ini_get( 'serialize_precision' );"

If we set serialize_precision to 17 it's behaviour changes:

ini_set( 'serialize_precision', 17 );

$value = 29.95;

var_dump( json_encode( $value ) );
remcotolsma commented 2 years ago

Added a test to reproduce this issue: https://github.com/pronamic/wp-number/commit/13e49a5da0f0ba621206a0a57d8dca8bdcf4ff32

Should we always set serialize_precision to -1 or should we simplify this by using (string) $float?

remcotolsma commented 2 years ago

I think we should go for the serialize_precision to -1 approach. The (string) $float behaviour can also be influenced via the precision option.

<?php

ini_set( 'serialize_precision', 17 );
ini_set( 'precision', 17 );

$value = 29.95;

var_dump( json_encode( $value ) );

var_dump( (string) $value );
Schermafbeelding 2021-11-16 om 09 08 23
remcotolsma commented 2 years ago

https://wiki.php.net/rfc/precise_float_value

Since we use json_encode we only have to temporary set serialize_precision to -1.

remcotolsma commented 2 years ago

Improved in https://github.com/pronamic/wp-number/compare/44c28cd0cdfb305e62a33ff4d05141d7430ccfad...e4682600b69906e005fe6aaa6af71febeea88d9b and https://github.com/wp-pay-extensions/gravityforms/commit/8b2bea4a3c0f3cd4a4aa5c87936eca17806dff9e.