catfan / Medoo

The lightweight PHP database framework to accelerate the development.
https://medoo.in
MIT License
4.83k stars 1.15k forks source link

float converting separator #255

Closed valix85 closed 3 years ago

valix85 commented 9 years ago

Hello i use php and mySQL with medoo version Medoo 0.9.8.3: 1) generate a notice on dsn variable (before with old version didn't appen), to fix i add 'database_type' => 'mysql', when i istance it, please specify in documentation homepage

2) most important i try to save in db a number with dot sperator (for example 8.33) and when i print last query executed by medoo i find 8,33 a number have comma and mysql rounded it to 8. Where / How i can modify number separator?

Thank you Valix85

catfan commented 9 years ago

Would you like to post the example code to demonstrate for that?

valix85 commented 9 years ago

Sorry i found that problem is it php json_decode convert 8.33 into 8,33 but i don't understand it...

{"0":{"min":1,"max":5,"price":10,"rid":12},"1":{"min":6,"max":50,"price":8.33,"rid":13}} after json_decode it change into array:

stdClass Object ( [0] => stdClass Object ( [min] => 1 [max] => 5 [price] => 10 [rid] => 12 )

[1] => stdClass Object
    (
        [min] => 6
        [max] => 50
        [price] => 8,33
        [rid] => 13
    )

)

carestad commented 6 years ago

I am also seeing this now. Floating point numbers have their decimal separator converted from a dot to commas somewhere in the code.

E.g. (from version 1.4.5): $medoo->debug()->insert('foo', ['number' => 5, 'float' => 5.5]);

Will produce the following output:

INSERT INTO "foo" ("number", "float") VALUES (5, 5,5)

I should also add that the query is able to run and insert, but the actual decimal is droped. So the value in the database is just 5.

In version 1.2.1 however, this is handled differently and the output with ->debug() is:

INSERT INTO "foo" ("number", "float") VALUES ('5', '5,5')

This is causing my insert to fail in 1.4.X versions of Medoo where I use floats. The error returned by ->error() is that the value count doesn't match the number of columns listed, which would be quite true considering the rewrite of 5.5 to 5,5 and unquoted will lead to just that.

carestad commented 6 years ago

After some research this seems to be related to locale settings. Setting the following in Medoo.php resolves the issue with floats:

setlocale(LC_NUMERIC, "en_US.UTF-8")