user6796243
user6796243

Reputation: 43

PHP convert a number to the power of

I have a sql database with figures such as 100, 1000, 10,000 etc.

Using php, how can I convert these figures so they are a power of 10?

e.g. 1,000 stored in database Needs to be displayed as 103

In case you are not aware of powers of 10, 103 = 10 × 10 × 10 (so 1000).

All figures in the database will be divisible by 10 and will have a power of 10. e.g. 10,000 (104).

Thanks in advance for any help.

If possible, please include a basic example.

Edit / Solution

Log10() is correct. So with my DB query I used:

10<sup>" . log10($row['number']) . "</sup>

Secondary problem

If a number is not a power of e.g. if someone enters 0 (this is the only number that can be entered that will not be a power of), it displays "-INF". Other than using a php if statment e.g.

if $row['number'] == 0

is there a way to not display the INF?

Upvotes: 0

Views: 288

Answers (1)

Indrasis Datta
Indrasis Datta

Reputation: 8618

You need to use log10() function.

If you think mathematically, log base 10 of 100 gives the exponent 2.

echo log10(1000); // 3

echo log10(1000000); // 6

Upvotes: 4

Related Questions