Michael Irwin
Michael Irwin

Reputation: 3149

Calculate precision

Let's say I have a number 10.5. Using PHP, I'd like to format it as 10.50. I know I can use number_format(10.5, 2). But the number of decimal places depends on a value subunit_to_unit, which in this case is equal to 100. If all I know is that subunit_to_unit is 100, how can I format 10.5 as 10.50?

Upvotes: 0

Views: 31

Answers (1)

Joey
Joey

Reputation: 354586

The logarithm of 100 to base 10 is 2. So you can just use log(subunit_to_unit, 10) to get your 2 here. You may want to floor the result, though, in case subunit_to_unit is something like 129.

Upvotes: 1

Related Questions