user3005775
user3005775

Reputation: 306

PHP decimal to egineering notation function

I have been looking up and down the PHP manual to find the answer to this, I even searched SO for it, I am looking for a function to convert any given decimal number to engineering notation to a fixed number of decimals.

for example

12345678 in engineering notation fixed to 2 decimals would be 12.35e6 489258239551 in engineering notation fixed to 5 decimals would be 489.25824e9

now I know about the sprintf function which gives me scientific notation and not engineering notation

echo sprintf("%.2e","12345678");

gives me 1.23e+7

echo sprintf("%.5e","489258239551");

gives me 4.89258e+11

nieither of those are in proper engineering format.

has anyone come across a function that does this?

Upvotes: 1

Views: 170

Answers (1)

Eli Hoto
Eli Hoto

Reputation: 19

If you do not need it to be reversible I recommend using substr.

http://php.net/manual/en/function.substr.php

Write a function that gets a number loads it into a string variable and returns sub string (substr) based on conditions

You can get the string length using strlen.

http://php.net/manual/en/function.strlen.php

Upvotes: 1

Related Questions