简体   繁体   English

将从excel读取的科学数字转换为csv

[英]Converting scientific number read from excel into csv

I am having an issue while reading from the excel sheet.我在阅读 Excel 表格时遇到问题。 I am getting my barcode values in scientific format in csv.我在 csv 中以科学格式获取我的条形码值。

How do i parse the original value instead of the scientific number ?我如何解析原始值而不是科学数?

Example : 9,96E+12 [In excel]示例:9,96E+12 [在 excel 中]

9960000000000 [I want this in CSV] 9960000000000 [我想要 CSV 格式的]

I am using fgetcsv function in php to parse the csv file.我在 php 中使用 fgetcsv 函数来解析 csv 文件。

while (($row = fgetcsv($handle, 1000, ';')) !== FALSE)
{
    if(!$header)
        $header = $row;
    else
        $data[] = array_combine($header, $row);
}

Output that i am getting :我得到的输出:

[0] => Array
    (
        [code] => FJT8-thr-thrtC2-VSXL
        [barcode] => 9960000000000
        [amount] => 70
        [status] => 1
    )

I want the output as我希望输出为

[0] => Array
    (
        [code] => FJT8-thr-thrtC2-VSXL
        [barcode] => 9,96E+12
        [amount] => 70
        [status] => 1
    )

You can use the floatval() and sprintf function to convert it back and forth as long as you convert the "," to "."您可以使用floatval()sprintf函数来回转换,只要将“,”转换为“.”即可。 before you use the number in PHP.在 PHP 中使用数字之前。

<?php

$start = '9,96E+12';
$str = floatval(str_replace(',','.',$start)); 

// Proof of concept
echo $start."<br />"; // starting off with 9,96E+12
echo $str."<br />"; // Converted number is 9960000000000
echo sprintf('%.2e',$str); // back to scientific number 9.96e+12


?>

Thank you Patrick Simard!!! 谢谢Patrick Simard !!!

Your method working correctly!!! 您的方法正常工作!!!

Big THANK YOU 非常感谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM