Jump to content

Preventing scientific notation in a string


Go to solution Solved by requinix,

Recommended Posts

How can I perform the following?  I expect sprintf might be related, but not sure how to make it flexible for numbers of different sizes.  Thanks

$const1=0.00000000000123;
$const2=1234123124123;

$string="some text $const1 more text $const2 more text;

echo($string);  //Desired: "some text 0.00000000000123 more text 1234123124123 more text;
Link to post
Share on other sites

Thanks requnix,  What about the ensuring that large numbers are not displayed in scientific notation? 

function returnNumberString($n, $min=20, $max=20){
    if(!is_numeric($n)) throw new Exception('Must be a number');
    $abs=abs($n);
    if(!$abs || $abs>1E-4 && $abs<1E4) {
        echo('just right (what should the "safe" bounds be?)'.PHP_EOL);
    }
    elseif($abs<1) {
        if($abs<pow(10,-$min)) throw new Exception("Must be greater than ".pow(10,-$min));
        echo('display small'.PHP_EOL);
        $n=rtrim(sprintf("%.{$min}F", $n), "0");
    }
    else {
        //What is the maximum size
        if($abs>pow(10,$max)) throw new Exception("Must be less than ".pow(10,$max));
        echo('display large'.PHP_EOL);
        $n=rtrim(rtrim(sprintf("%.{$max}F", $n), "0"),'.');
    }
    return $n;
}
function t($n) {
    echo"Value: $n".PHP_EOL;
    try{
        $n=returnNumberString($n);
        echo"some text $n more text".PHP_EOL;
    }
    catch(Exception $e) {
        echo $e->getMessage().PHP_EOL;
    }
}
t(1.23E-2);
t(1.23E-6);
t(1.23E-10);
t(1.23E-18);
t(1.23E-28);
t(1.23E2);
t(1.23E6);
t(1.23E19);
t(1.23E40);
t(0);
Link to post
Share on other sites
Value: 0.0123
just right (what should the "safe" bounds be?)
some text 0.0123 more text
Value: 1.23E-6
display small
some text 0.00000123 more text
Value: 1.23E-10
display small
some text 0.000000000123 more text
Value: 1.23E-18
display small
some text 0.00000000000000000123 more text
Value: 1.23E-28
Must be greater than 1.0E-20
Value: 123
just right (what should the "safe" bounds be?)
some text 123 more text
Value: 1230000
display large
some text 1230000 more text
Value: 1.23E+19
display large
some text 12300000000000000000 more text
Value: 1.23E+40
Must be less than 1.0E+20
Value: 0
just right (what should the "safe" bounds be?)
some text 0 more text
All the "some text" outputs aren't using scientific notation. What are you talking about?
Link to post
Share on other sites

Sorry, I had included more in the previous post, but must have accidentally deleted it.  Get quirky results sometimes such as when using the following due to float rounding errors.

t(1234567.86543); //outputs some text 1234567.86543000000528991222 more text

I guess if greater than 1, I can cast it to an integer, use strlen to get the number of digits, subtract that from the number of formatted decimals, remove trailing zeros, and then remove a trailing period (if applicable).  Just seems, however, that there should be a more straightforward way to do this.

 

 

Regarding these lines, any recommendation what the bounds should be so that PHP will never use scientific notation if concatenated to a string?  Will that change if the value is positive or negative?  If it does change, I suppose I could perform the operations using the absolute values and then manually add a minus sign.  Again, a bit of a kludge.

if(!$abs || $abs>1E-4 && $abs<1E4) {
        echo('just right (what should the "safe" bounds be?)'.PHP_EOL);
}

PS.  Purpose is to dynamically generate SQL type queries for a specialty database which doesn't allow SELECT someField * 3.39472E-6 AS alias FROM ....

 

Link to post
Share on other sites

That's your standard floating-point error. round() to a number of decimal places you know you'll never need to go beyond.

 

But are you sure scientific notation isn't allowed? MySQL, SQL Server, and PostgreSQL all do.

Link to post
Share on other sites

I'm not positive, but I couldn't find any documentation stating that I could and posted a question but haven't had any replies.  https://community.influxdata.com/t/how-to-use-scientific-notation-in-a-select-query-mathematical-operator/4743

 

Off topic for PHP, but can you confirm that my previous example (duplicated below) is correct for MySQL?  Couldn't find much on the subject, however, https://dev.mysql.com/doc/refman/5.6/en/number-literals.html  suggests I am doing it correct.  Most other things I read were just people asking how to disable presenting output such format.  Thanks

SELECT someField * 3.39472E-6 AS alias FROM ....
Link to post
Share on other sites

https://docs.influxdata.com/influxdb/v1.5/query_language/spec/#literals

Floats

InfluxQL supports floating-point literals. Exponents are not currently supported.

 

float_lit = int_lit "." int_lit .

And I wouldn't worry about floating-point errors. It's all equally imprecise in the end.

 

I don't have a MySQL console in front of me, but yes that query would be perfectly fine.

  • Like 1
Link to post
Share on other sites
This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.