Skip to content

Instantly share code, notes, and snippets.

@ausi
Created October 11, 2018 13:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ausi/3b244e836ac680a6c3f4b3bb1f82f85d to your computer and use it in GitHub Desktop.
Save ausi/3b244e836ac680a6c3f4b3bb1f82f85d to your computer and use it in GitHub Desktop.
Convert number/integer/float into string without exponential notation
<?php
declare(strict_types=1);
function numberToString($number, $precision = null)
{
if (is_int($number)) {
return (string) $number;
}
if ($precision === null) {
$precision = (int) ini_get('precision');
}
$sign = '';
$number = sprintf('%.' . ($precision - 1) . 'e', (float) $number);
if ($number[0] === '-') {
$sign = '-';
$number = substr($number, 1);
}
if ($number[1] !== '.') {
throw new InvalidArgumentException(
sprintf('Unable to convert "%s" into a string representation.', $number)
);
}
$number = explode('e', $number);
$digits = rtrim($number[0][0] . substr($number[0], 2), '0');
$shift = (int) $number[1] + 1;
if ($shift < 1) {
$result = '0.'.str_repeat('0', ($shift) * -1).$digits;
}
elseif (strlen($digits) <= $shift) {
$result = str_pad($digits, $shift, '0');
}
else {
$result = substr($digits, 0, $shift).'.'.substr($digits, $shift);
}
return $sign.$result;
}
@ausi
Copy link
Author

ausi commented Nov 29, 2018

alternative:

<?php

declare(strict_types=1);

function numberToString(float $number, ?int $precision = null)
{
    if ($precision === null) {
        $precision = (int) ini_get('precision');
    }
    
    if (!preg_match(
        '/^(-?)(\d)\.(\d+)e([+-]\d+)$/',
        sprintf('%.' . ($precision - 1) . 'e', (float) $number), 
        $match
    )) {
        throw new InvalidArgumentException(
            sprintf('Unable to convert "%s" into a string representation.', $number)
        );
    }

    $digits = rtrim($match[2] . $match[3], '0');
    $shift = (int) $match[4] + 1;
    
    return $match[1] . rtrim(
        (substr(str_pad($digits, $shift, '0'), 0, max(0, $shift)) ?: '0')
            . '.' . str_repeat('0', max(0, -$shift))
            . substr($digits, max(0, $shift)),
        '.'
    );
}

echo numberToString(0.0) . "\n";
echo numberToString(-0.0) . "\n";
echo numberToString(1.0) . "\n";
echo numberToString(-1.0) . "\n";
echo numberToString(0.1) . "\n";
echo numberToString(0.1 + 0.2 + 0.1 + 0.2) . "\n";
echo numberToString(0.066) . "\n";
echo numberToString(0.0000000000000000000000000000000000000000000000000000001) . "\n";
echo numberToString(1.0) . "\n";
echo numberToString(1.1) . "\n";
echo numberToString(10000000.0) . "\n";
echo numberToString(10000000.1) . "\n";
echo numberToString(10000000.01) . "\n";
echo numberToString(1234567890123456789012345678901234567890123456789012345678901234567890) . "\n";
echo numberToString(PHP_INT_MAX) . "\n";
echo numberToString(9000000000000000000) . "\n";
echo numberToString(9000000000000000000.0) . "\n";
echo numberToString((float)PHP_INT_MAX) . "\n";
echo numberToString(4503599627370496) . "\n";
echo numberToString(4503599627370496.0) . "\n";
echo numberToString(1.2345678901234567890123456789) . "\n";
echo (string) 1.2345678901234567890123456789 . "\n";
echo numberToString(PHP_INT_MAX * PHP_INT_MAX) . "\n";
echo numberToString(PHP_FLOAT_EPSILON) . "\n";
echo numberToString(0.001+PHP_FLOAT_EPSILON) . "\n";
echo numberToString(PHP_FLOAT_MIN) . "\n";
echo numberToString(PHP_FLOAT_MAX) . "\n";
#echo numberToString(INF) . "\n";
#echo numberToString(NAN) . "\n";

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment