CFormatter使用simple-csv-export扩展名丢失小数

时间:2014-06-17 11:33:33

标签: php csv yii yii-extensions

我通过此链接http://www.yiiframework.com/wiki/401/simple-csv-export上传csv 并发现错误使格式在双值中丢失小数。

该课程看起来像这样

<?php

class MyFormatter extends CFormatter
{
...
   public function formatMoney($value)
   {
        FilesFuncs::DebToFile('formatMoney  $value::' . print_r($value, true), false); // debugging to file : i see result double values WITHOUT decimals
        return round(floatval($value),2);
   }
...

在控制中我跑:

$dst_filename= 'registers-upto--' . strftime("%Y-%m-%d-%H-%M-%S") . ".csv";
CsvExport::export( $GoodsList,
array(  'good_id'=>array('number' ,'fixed8' ),
'name'=>array(),
'price'=>array('number' ,'money'  ), // MONEY TYPE
'discount'=>array('number' ,'fixed8' ),

在CsvExport.php文件中:

foreach ($rows as $row) {
  $r = '';
  foreach ($coldefs as $col => $config) {

    if (isset($row[$col])) {
      $val = $row[$col];
       FilesFuncs::DebToFile('$col::' . print_r($col, true), false);
       FilesFuncs::DebToFile('$val::' . print_r($val, true), false); // HERE $val is WITH decimals
       foreach ($config as $conf)
         if (!empty($conf)) {
           $val = Yii::app()->format->format( $val, $conf ); // debugging to file : i see result double values returns  WITHOUT decimals
...

在数据库字段中描述为价格双倍(12,2)。什么是问题以及如何解决它?

我正在使用Yii 1.1.14

1 个答案:

答案 0 :(得分:0)

也许您的数字格式使用逗号(',')来区分数字和小数位?在这种情况下,您需要将数字用双引号(")括起来,这样CSV文件就不会认为数字中的逗号是分隔符。

此外,在$separator类中设置CsvExport个选项(或创建一个子类),使用","以外的其他选项来避免错误。