Bug #75920 [Nab]: precision problem with decimals read from sql server

From: Date: Tue, 06 Feb 2018 08:59:43 +0000
Subject: Bug #75920 [Nab]: precision problem with decimals read from sql server
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-213826@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=75920&edit=1 ID: 75920 User updated by: km dot wrona at gmail dot com Reported by: km dot wrona at gmail dot com Summary: precision problem with decimals read from sql server Status: Not a bug Type: Bug Package: PDO DBlib Operating System: Alpine PHP Version: 7.1.14 Block user comment: N Private report: N New Comment: Thank you for your explanation, except I am not sure if you've read my post. 1) That 1.6 and any other works OK and is represented properly when defined in php code 2) That 1.6 is represented properly when the column in SqlServer table is defined as float (or when a value is casted to float) 3) That 1.6 IS NOT represented properly when the column in SqlServer table is defined as decimal(10,2) (or when a value is casted to decimal) Previous Comments: ------------------------------------------------------------------------ [2018-02-06 06:30:22] rasmus@php.net Floating point values have a limited precision. Hence a value might not have the same string representation after any processing. That also includes writing a floating point value in your script and directly printing it without any mathematical operations. If you would like to know more about "floats" and what IEEE 754 is, read this: http://www.floating-point-gui.de/ Thank you for your interest in PHP. 1.6 can't be represented exactly as a floating point value. ------------------------------------------------------------------------ [2018-02-05 13:29:42] km dot wrona at gmail dot com Description: ------------ The number is not represented properly when the column is defined as decimal. Test script: --------------- var_dump($this->selectRaw('CAST(\'1.6\' as decimal(10,2)) as dec, CAST(\'1.6\' as float) as flo')->limit(1)->get()); exit; array(1) { [0]=> object(stdClass)#789 (2) { ["dec"]=> float(1.5999999999999) ["flo"]=> float(1.6) } } //default behaviour ["dec"]=> float(1.5999999999999) //with //ini_set('precision', 25); //ini_set('serialize_precision', 25); ["dec"]=> float(1.599999999999909050529823) Expected result: ---------------- 1.6 no matter if decimal or float ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=75920&edit=1

« previous php.bugs (#213826) next »