Java Math log10() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Java 5+

Definition

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
CallWhat it does
Math.log10(100)Returns exactly 2.0.
Math.log10(0)Returns -Infinity.
(int) Math.log10(n) + 1Number 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

ParameterDescription
aThe value whose base-10 logarithm you want. It must be greater than zero for a normal result.

Return Value

Return ValueDescription
doubleThe 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.

Compare with Similar Methods

Other logarithm tools:

MethodHow it differs
log(x)Natural logarithm (base e); use it for growth and decay.
log1p(x)log(1 + x) accurate for tiny x.
pow(10, n)The inverse: raises 10 to a power.
String.valueOf(n).length()Another way to count digits, with no floating-point math.

Java Examples

Example 1 — Exact Powers of Ten and the Special Values

Take the logarithm of 1, 10, 100, 1000, and a million, then try zero, a negative number, and infinity.

Java
public class MathLog10Basics {
    public static void main(String[] args) {
        int[] powers = {1, 10, 100, 1000, 1000000};
        for (int p : powers) {
            System.out.println("log10(" + p + ") = " + Math.log10(p));
        }
        System.out.println("log10(0) = " + Math.log10(0));
        System.out.println("log10(-5) = " + Math.log10(-5));
        System.out.println("log10(Infinity) = " + Math.log10(Double.POSITIVE_INFINITY));
    }
}

How It Works

  1. 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.
  2. 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.
  3. 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));
        }
    }
}

How It Works

  1. 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.
  2. 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.
  3. 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.

Java
import java.util.Locale;

public class MathLog10Scales {
    public static void main(String[] args) {
        double thousand = 10 * Math.log10(1000);
        double double2x = 10 * Math.log10(2);
        System.out.println("1000x power: " + String.format(Locale.US, "%.1f", thousand) + " dB");
        System.out.println("2x power:    " + String.format(Locale.US, "%.2f", double2x) + " dB");

        double hydrogen = 3.2e-4;
        double ph = -Math.log10(hydrogen);
        System.out.println("pH of 0.00032 mol/L: " + String.format(Locale.US, "%.2f", ph));
    }
}

How It Works

  1. Ten times the logarithm. Power ratios are measured in decibels as 10 * log10(ratio); a ratio of 1000 has logarithm 3, which gives 30.0 dB.
  2. Doubling adds 3. log10(2) is about 0.30103, so doubling the power always adds about 3.01 dB, a number worth remembering.
  3. pH is a negative logarithm. For 3.2e-4 the logarithm is about -3.49, and the leading minus sign turns it into the positive acidity value 3.49.

Frequently Asked Questions

It returns the base-10 logarithm of a number: the power of 10 that gives it. Math.log10(1000) is 3.0.
For n greater than 0, use (int) Math.log10(n) + 1. Handle 0 separately (it has 1 digit), and use Math.abs for negative numbers.
Math.log is the natural logarithm (base e), while Math.log10 is base 10. Math.log(100) is about 4.6052, but Math.log10(100) is exactly 2.0.
It returns -Infinity. Math.log10 of a negative number returns NaN. Neither throws an exception.
Java 5 (JDK 1.5). Before that, Math.log(x) / Math.log(10) was used, which can be off by a tiny amount.

Next: log1p()

Compute ln(1 + x) accurately for x close to zero.

log1p() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful