Friday, November 23, 2018

PHP sample code to parse number values in exponential format


In a search or get operation, number/currency values are returned in exponential format if the value exceeds more than 8 digits.

As an example, if a Sales Order transaction has an amount of 10,000,000, it will be returned as "1.E07" in Web Services get or search operation. This is in accordance with the SOAP Specification detailed here: http://www.w3.org/TR/xmlschema-2/#double

----
3.2.5.1 Lexical representation

double values have a lexical representation consisting of a mantissa followed, optionally, by the character "E" or "e", followed by an exponent. The exponent ·must· be an integer. The mantissa must be a decimal number. The representations for exponent and mantissa must follow the lexical rules for integer and decimal. If the "E" or "e" and the following exponent are omitted, an exponent value of 0 is assumed.

The special values positive and negative infinity and not-a-number have lexical representations INF, -INF and NaN, respectively. Lexical representations for zero may take a positive or negative sign.

For example, -1E4, 1267.43233E12, 12.78e-2, 12 , -0, 0 and INF are all legal literals for double.
----


To be able to deal with this on PHP, you need to use number_format: a built-in PHP function that formats a number with grouped thousands. Here's a sample code:

require_once '../PHPtoolkit.php';
require_once 'login_info.php';

global $myNSclient;
$myNSclient->setPreferences( true );
$internalId = 3752;

$vendorBillRecord = $myNSclient->get(new nsRecordRef(array('type' => "vendorBill",'internalId' => $internalId)));
$total = $vendorBillRecord->record->getField('userTotal');

echo number_format($total, 2, '.',',');

No comments:

Post a Comment