开发者

Remove extra decimal place in float datatype on symfony?

I have in my database the ff. float:

1
1.5
1.7
2

I need to keep this as is in the program, but somehow symfony automatically convert these to ff by default:

1.00
1.50
1.70
2.00

My schema:

chapterno:
  t开发者_开发问答ype: float(4)
  fixed: false
  unsigned: true
  primary: false
  notnull: true
  autoincrement: false

xdebug trace before and after the function call:

0.4769   12560704                                 -> sfOutputEscaperIteratorDecorator->__construct() G:\Duc\xampp\htdocs\msym\lib\vendor\symfony\lib\escaper\sfOutputEscaper.class.php:125
0.4770   12560704                                   -> sfOutputEscaper->__construct() G:\Duc\xampp\htdocs\msym\lib\vendor\symfony\lib\escaper\sfOutputEscaperIteratorDecorator.class.php:49
0.4770   12560944                                   -> IteratorIterator->__construct() G:\Duc\xampp\htdocs\msym\lib\vendor\symfony\lib\escaper\sfOutputEscaperIteratorDecorator.class.php:51
0.4771   12560944                                     -> Doctrine_Record->getIterator() G:\Duc\xampp\htdocs\msym\lib\vendor\symfony\lib\plugins\sfDoctrinePlugin\lib\vendor\doctrine\Doctrine\Record.php:0
0.4771   12561384                                       -> Doctrine_Record_Iterator->__construct() G:\Duc\xampp\htdocs\msym\lib\vendor\symfony\lib\plugins\sfDoctrinePlugin\lib\vendor\doctrine\Doctrine\Record.php:2166
0.4771   12561384                                         -> Doctrine_Record->getData() G:\Duc\xampp\htdocs\msym\lib\vendor\symfony\lib\plugins\sfDoctrinePlugin\lib\vendor\doctrine\Doctrine\Record\Iterator.php:53
0.4771   12561416                                         -> ArrayIterator->__construct() G:\Duc\xampp\htdocs\msym\lib\vendor\symfony\lib\plugins\sfDoctrinePlugin\lib\vendor\doctrine\Doctrine\Record\Iterator.php:53
0.4771   12562056                             -> sfOutputEscaperIteratorDecorator->key() G:\Duc\xampp\htdocs\msym\lib\vendor\symfony\lib\escaper\sfOutputEscaperIteratorDecorator.class.php:0
0.4772   12562088                               -> IteratorIterator->key() G:\Duc\xampp\htdocs\msym\lib\vendor\symfony\lib\escaper\sfOutputEscaperIteratorDecorator.class.php:81
0.4772   12560272                             -> sfOutputEscaperIteratorDecorator->getChapterno() G:\Duc\xampp\htdocs\msym\apps\frontend\modules\book\templates\indexSuccess.php:5
0.4772   12560424                               -> sfOutputEscaperObjectDecorator->__call() G:\Duc\xampp\htdocs\msym\lib\vendor\symfony\lib\escaper\sfOutputEscaperObjectDecorator.class.php:0
0.4772   12560456                                 -> count() G:\Duc\xampp\htdocs\msym\lib\vendor\symfony\lib\escaper\sfOutputEscaperObjectDecorator.class.php:47
0.4772   12560672                                 -> call_user_func_array() G:\Duc\xampp\htdocs\msym\lib\vendor\symfony\lib\escaper\sfOutputEscaperObjectDecorator.class.php:64
0.4773   12561072                                   -> sfDoctrineRecord->__call() G:\Duc\xampp\htdocs\msym\lib\vendor\symfony\lib\plugins\sfDoctrinePlugin\lib\record\sfDoctrineRecord.class.php:0
0.4773   12561200                                     -> substr()

I don't need this kind of functionality, how can i disable this?


Looks like a bug to me. Casting to float solves it. Just do this:

echo (float) $chapter->getChapterNo();

You may also override getChapterNo() to do this for you (use _get in your override to avoid infinite loops with __call().

UPDATE I reported this here, so you can subscribe if you want.


Maybe you need this type in your schema:

chapterno: { type: decimal, size: 2, scale: 1 }

To remove .0 from the number you can use this php code:

preg_match("/([0-9](\.[1-9]))|[0-9]/", $chapterno, $match);

Of course $match[0] is the needed result.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜