Quantcast
Channel: Maesan blog » PHP
Viewing all articles
Browse latest Browse all 9

[cakePHP] floatのフィールドが指数表記になっちゃう

$
0
0

cakePHPでfloatのフィールドを使った場合、7桁を超える数値を入れたらフォームで指数表記されちゃうのが困る。

これが

こうなる

仕方ないのでviewで

$this->data["Article"]["point"] = sprintf('%.2f', $this->data["Article"]["point"]);

って書いたらいけるかと思ったらいけなかった。
よくよく考えたらviewをレンダリングする前にformが出来上がってんじゃなかったっけ?w
ってことに気付いて素直にcontrollerに書いたら期待通りに動いた。

function edit($id = null) {
if (!$id && empty($this->data)) {
$this->Session->setFlash(__('Invalid article', true));
$this->redirect(array('action' => 'index'));
}
if (!empty($this->data)) {
if ($this->Article->save($this->data)) {
$this->Session->setFlash(__('The article has been saved', true));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The article could not be saved. Please, try again.', true));
}
}
if (empty($this->data)) {
$this->data = $this->Article->read(null, $id);
$this->data["Article"]["point"] = sprintf('%.2f', $this->data["Article"]["point"]);
}
}


何かイマイチすっきりしないのですが、他の解決策ないのかねぇ?


Viewing all articles
Browse latest Browse all 9

Trending Articles