In Java, Math.log10(x) returns the base-10 logarithm of x: the power of 10 that gives x. Math.log10(1000) is 3.0.
It is the natural choice for counting digits, for scales that grow by factors of ten (decibels, pH, earthquake magnitude), and for orders of magnitude. It was added in Java 5 (JDK 1.5).
A static method of the java.lang.Math class, added in Java 5 (JDK 1.5). Call it on the class name, as in Math.log10(...). No import is needed, because classes in java.lang are always visible.
Syntax
Pass a positive number. The result is a double.
Java
double three = Math.log10(1000); // 3.0
int digits = (int) Math.log10(n) + 1; // digits in n > 0
double db = 10 * Math.log10(powerRatio); // decibels
Call
What it does
Math.log10(100)
Returns exactly 2.0.
Math.log10(0)
Returns -Infinity.
(int) Math.log10(n) + 1
Number of digits of a positive n.
-Math.log10(h)
pH from a concentration h.
Java 5 or newer is needed; before that, people wrote Math.log(x) / Math.log(10), which can miss a whole number by a hair (2.9999999999999996 for 1000 on common JVMs). log10 avoids that for exact powers of ten.
Library declarations
Java
// java.lang.Math (Java 5+)
public static double log10(double a)
Parameters
Parameter
Description
a
The value whose base-10 logarithm you want. It must be greater than zero for a normal result.
Return Value
Return Value
Description
double
The base-10 logarithm of a. If a is exactly 10 to a whole-number power n, the result is exactly n. NaN for negative or NaN input, -Infinity for a zero, Infinity for Infinity.
Count the zeros. The number 1000 is 10 to the power 3, so its base-10 logarithm is 3; each value in the loop is exactly a power of ten.
Exact, not approximate. Java promises a whole-number answer for exact powers of ten, so you see 3.0 and 6.0 rather than 2.9999999999999996.
Outside the domain. No power of ten gives 0 or a negative number, so zero gives -Infinity and -5 gives NaN; int arguments are widened to double for each call.
Example 2 — Count the Digits of a Number
The whole part of the logarithm, plus one, is the digit count. Guard zero, and take the absolute value of negatives.
Java
public class MathLog10Digits {
static int digits(int n) {
if (n == 0) {
return 1;
}
return (int) Math.log10(Math.abs((long) n)) + 1;
}
public static void main(String[] args) {
int[] numbers = {7, 99, 100, 123456, 999999999, -2500, 0};
for (int n : numbers) {
System.out.println("digits(" + n + ") = " + digits(n));
}
}
}
Whole part plus one.log10(99) is about 1.996, whose whole part is 1, so 99 has 2 digits; log10(100) is exactly 2.0, so 100 has 3.
Close calls stay correct. 999999999 has a logarithm just under 9 (about 8.9999999996), so the cast gives 8 and the result is 9 digits, not 10.
Special inputs handled first. Zero would produce -Infinity, so it returns 1 directly; the negative value is made positive with Math.abs (on a long, so even Integer.MIN_VALUE is safe).
Example 3 — Decibels and pH on a Log Scale
Scales that grow by factors of ten are logarithms. Format the results with an English-style decimal point.