数据库中表格小数域的记录不正确
Incorrect record in DB of decimal field of the form
我想在数据库中写入值从'last_price' = 9.12345678(点后需要8个字符)
CakePHP - last 3.6.7, PHP 7.2x64, MySQL 5.7x64, 字段 'last_price' 在数据库中输入:DECIMAL 20, 8
控制器:
public function add ()
{
// debug ($ this-> request-> getData ());
$ pair = $ this-> Pairs-> newEntity ();
if ($ this-> request-> is ('post')) {
$ pair = $ this-> Pairs-> patchEntity ($ pair, $ this-> request-> getData ());
// debug ($ pair);
if ($ this-> Pairs-> save ($ pair)) {
$ this-> Flash-> success (__ ('The pair has been saved.'));
return $ this-> redirect (['action' => 'index']);
}
$ this-> Flash-> error (__ ('The pair could not be saved. Please, try again.'));
}
$ exchanges = $ this-> Pairs-> Exchanges-> find ('list', ['limit' => 200]);
$ this-> set (compact ('pair', 'exchanges'));
}
内容$this->request->getData():
[
'exchange_id' => '1',
'title' => '12121',
'last_price' => '9.12345678',
'favorites' => '0'
]
PairsTable.php(型号)
$ validator
-> decimal ('last_price', 8)
// -> maxLength ('last_price', 30)
-> requirePresence ('last_price', 'create')
-> notEmpty ('last_price');
我点击添加,但以下查询正在进入数据库:
2018-07-24 07:09:19 Debug: duration = 0 rows = 1 INSERT INTO pairs (exchange_id, title, last_price, favorites, created, modified) VALUES (1, '5453534534543', '9.123457', 0, '2018 -07-24 07:09:19 ',' 2018-07-24 07:09:19 ')
为什么 'last_price' = 9.123457 ?!!!????
我明白了-它在数据库中写为 9.12345700
为什么不是 9.12345678 ??
为了帮助您解决这个问题,我设置了一个 table 和 decimal_value (decimal(18,12)), float_value (float(18,12)), double_value (双(18,12)).
double_value 在所有三个插入中都是准确的。
将列类型更改为 DOUBLE。清除 orm_cache.
我想在数据库中写入值从'last_price' = 9.12345678(点后需要8个字符)
CakePHP - last 3.6.7, PHP 7.2x64, MySQL 5.7x64, 字段 'last_price' 在数据库中输入:DECIMAL 20, 8
控制器:
public function add ()
{
// debug ($ this-> request-> getData ());
$ pair = $ this-> Pairs-> newEntity ();
if ($ this-> request-> is ('post')) {
$ pair = $ this-> Pairs-> patchEntity ($ pair, $ this-> request-> getData ());
// debug ($ pair);
if ($ this-> Pairs-> save ($ pair)) {
$ this-> Flash-> success (__ ('The pair has been saved.'));
return $ this-> redirect (['action' => 'index']);
}
$ this-> Flash-> error (__ ('The pair could not be saved. Please, try again.'));
}
$ exchanges = $ this-> Pairs-> Exchanges-> find ('list', ['limit' => 200]);
$ this-> set (compact ('pair', 'exchanges'));
}
内容$this->request->getData():
[
'exchange_id' => '1',
'title' => '12121',
'last_price' => '9.12345678',
'favorites' => '0'
]
PairsTable.php(型号)
$ validator
-> decimal ('last_price', 8)
// -> maxLength ('last_price', 30)
-> requirePresence ('last_price', 'create')
-> notEmpty ('last_price');
我点击添加,但以下查询正在进入数据库:
2018-07-24 07:09:19 Debug: duration = 0 rows = 1 INSERT INTO pairs (exchange_id, title, last_price, favorites, created, modified) VALUES (1, '5453534534543', '9.123457', 0, '2018 -07-24 07:09:19 ',' 2018-07-24 07:09:19 ')
为什么 'last_price' = 9.123457 ?!!!???? 我明白了-它在数据库中写为 9.12345700
为什么不是 9.12345678 ??
为了帮助您解决这个问题,我设置了一个 table 和 decimal_value (decimal(18,12)), float_value (float(18,12)), double_value (双(18,12)).
double_value 在所有三个插入中都是准确的。
将列类型更改为 DOUBLE。清除 orm_cache.