influxdata / influxdb-php

influxdb-php: A PHP Client for InfluxDB, a time series database
http://influxdb.com
MIT License
432 stars 125 forks source link

Fix locale floats #137

Closed pdelre closed 5 years ago

pdelre commented 5 years ago

When running under locales with a non-. decimal_point separator, PHP generates a bad line protocol string.

For example:

require 'vendor/autoload.php';

$p = new InfluxDB\Point('foo', 1.11, [], ['bar' => 'baz']);

setlocale(LC_ALL, 'en_US');
echo (string) $p . PHP_EOL;

setlocale(LC_ALL, 'de_DE');
echo (string) $p . PHP_EOL;

Results in two different lines:

foo bar="baz",value=1.11
foo bar="baz",value=1,11

This change adds test coverage for this scenario and resolves the issue by ensuring a . decimal_point separator compatible locale is used at time of string casting.

gianarb commented 5 years ago

Thanks a lot! Even more for the test, you wrote! I think this is the function we should use other than swapping the locale function https://www.php.net/manual/en/function.number-format.php

pdelre commented 5 years ago

I had started with number_format but there could be a loss of precision due to $decimals (or extraneous data sent).

number_format(1.11);
number_format(1.11, 0, '.', '');
number_format(1.11, 10, '.', '');

Results in

'1'
'1'
'1.1100000000'
gianarb commented 5 years ago

Yep, that's what I was trying right now! Ok! I will double-check it tomorrow and merge it tomorrow morning! (I am not in front of my laptop atm!)

Thanks a lot

pdelre commented 4 years ago

@gianarb Is there a future release scheduled? We just ran into this bug again in 1.15.0 released May 2019.