Link to home
Start Free TrialLog in
Avatar of worldofwires
worldofwiresFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Engineering Units in PHP

Hi,

I'm having issues when exporting data to a csv file via the fopen method. Any large float (above about 20million) is being converted into engineering units. Not a problem if the destination prog was Excel but it's not and it's error as a string.

I'm currently writing a hack to covert big numbers into integers, losing the accuracy but not much (they're currency values).

Thanks for any help,
John
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

What do you mean by the "fopen method?"  Can you show us the code and a bit of test data?  Thanks, ~Ray
Avatar of worldofwires

ASKER

Ray,

I don't really have any test data as it's confidential information. Just a rows of number between 1 and 25 million with 2 decimal places will prove the point. The code is something like...
if($f=fopen($oslocale."data/dumps/".$fn.".csv", "w")) {
	fputcsv($f, $apro, ',', '"');
}
fclose($f);

Open in new window


My hack seems to work so I suspect it's just the length of the floatval() that caused the issue. 2 DP when over a million hardly seems worrying about when talking about currency.

Thanks,
John

P.S. Also, the $apro array could be...
$apro=array(floatval(25000000.00), intval(25000000));

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Run this and see if you get the same thing on your server that I get here:
http://www.laprbass.com/RAY_temp_worldofwires.php
<?php // RAY_temp_worldofwires.php
error_reporting(E_ALL);
echo "<pre>";

$apro=array(floatval(25000000.00), intval(25000000));
// var_dump($apro);

// WHAT IS THE MAXIMUM INTEGER ON THIS SERVER?
var_dump(PHP_INT_MAX);

Open in new window

Ray,

It returns: int(2147483647).

The problem seems to be resolved when converting floats to ints for the bigger values.

Thanks for your help,
John
Thanks for the points - it's a great question!  All the best, ~Ray