C# Math Log10() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Math

Definition

In C#, Math.Log10(d) returns the base-10 logarithm of a number. It answers: "10 to which power gives this number?". Math.Log10(1000) is 3, because 10 x 10 x 10 is 1000.

Think of the number of zeros. 100 has 2 zeros and its log is 2. 1000 has 3 zeros and its log is 3. Numbers in between give fractions: Math.Log10(500) is about 2.7. Base 10 matches our everyday counting, so it is used for the number of digits, sound levels in decibels, and the acidity (pH) scale. For other bases, see Log() and Log2().

This is a static member of the static class System.Math. You call it with the class name, such as Math.Log10(...). There is no Math object to create. All the work is done by the method you call, and it never changes your variables. Log10 is in every .NET version. It takes and returns double.

Syntax

Pass one number greater than zero. The answer is a double.

C#
double a = Math.Log10(1000);   // 3
double b = Math.Log10(1);      // 0
double c = Math.Log10(0);      // -Infinity
CallWhat it does
Math.Log10(1)Returns 0.
Math.Log10(1000)Returns 3.
Math.Log10(0)Returns -Infinity.
Math.Log10(-1)Returns NaN. No real power of 10 gives a negative number.

Exact powers of ten (1, 10, 100, 1000, ...) give whole-number answers. For other numbers, the answer is a long decimal, so round it before showing it. Zero gives -Infinity, a negative number gives NaN, and neither throws an exception. This method equals Math.Log(d, 10) but is more direct.

Library declarations
C#
public static double Log10(double d);

Parameters

ParameterDescription
dRequired. The number whose base-10 logarithm you want. It must be greater than 0 for a normal answer.

Return Value

Return ValueDescription
doubleThe base-10 logarithm of d. -Infinity for 0, NaN for a negative number or NaN, Infinity for positive infinity.

C# Examples

Example 1 — Logarithms of Powers of Ten

Call Log10 on 1, 10, 100, and 1000, and on zero.

C#
using System;

class Program {
    static void Main() {
        Console.WriteLine(Math.Log10(1));
        Console.WriteLine(Math.Log10(10));
        Console.WriteLine(Math.Log10(100));
        Console.WriteLine(Math.Log10(1000));
        Console.WriteLine(Math.Log10(0));
    }
}

How It Works

  1. 1 gives 0. 10 to the power 0 is 1, so Math.Log10(1) is 0.
  2. 10 gives 1. 10 to the power 1 is 10. The answer is 1.
  3. 100 and 1000. Each extra zero adds 1 to the answer, giving 2 and 3.
  4. Zero. No power of 10 reaches 0, however negative. The answer is -Infinity.

Example 2 — How Many Digits Does a Number Have?

Use Floor and Log10 together to count the digits of positive whole numbers.

C#
using System;

class Program {
    static void Main() {
        int[] numbers = { 12345, 999, 1000, 7 };

        foreach (int n in numbers) {
            int digits = (int)Math.Floor(Math.Log10(n)) + 1;
            Console.WriteLine(digits);
        }
    }
}

How It Works

  1. The idea. A number with d digits is at least 10 to the power d - 1, so its base-10 logarithm is between d - 1 and d.
  2. Floor then add 1. Math.Log10(12345) is about 4.09. Floor gives 4, and 4 + 1 is 5 digits.
  3. At the edges. 999 gives about 2.9996, floor 2, so 3 digits. 1000 gives exactly 3, so 4 digits. The jump happens exactly at powers of ten.
  4. One digit. Math.Log10(7) is about 0.85, floor 0, plus 1 is 1. Zero would break this (log of 0 is infinite), so test for it first.

Example 3 — Decibels, pH, and Bad Input

Use Log10 for two real formulas, then see the result for a negative number and for infinity.

C#
using System;

class Program {
    static void Main() {
        double powerRatio = 1000;
        double decibels = 10 * Math.Log10(powerRatio);
        Console.WriteLine(decibels);

        double hydrogen = 0.001;
        double ph = -Math.Log10(hydrogen);
        Console.WriteLine(Math.Round(ph, 6));

        Console.WriteLine(Math.Log10(-1));
        Console.WriteLine(Math.Log10(double.PositiveInfinity));
    }
}

How It Works

  1. Decibels. Sound power ratios are measured as 10 x log10 of the ratio. A ratio of 1000 is 10 x 3, which is 30 dB.
  2. pH. The acidity of a liquid is minus the base-10 logarithm of its hydrogen concentration. 0.001 is 10 to the power -3, so the pH is 3.
  3. Round for safety. 0.001 cannot be stored exactly in a double, so we round the pH to 6 places to avoid showing a tiny error.
  4. Edge cases. A negative number gives NaN, and positive infinity gives Infinity.

Frequently Asked Questions

It returns the base-10 logarithm of a number. Math.Log10(1000) is 3 because 10 to the power 3 is 1000.
Use (int)Math.Floor(Math.Log10(n)) + 1 for positive whole numbers. Another way is n.ToString().Length, which is simple and also handles zero.
Log uses base e (about 2.718) and Log10 uses base 10. Math.Log(100) is about 4.605 and Math.Log10(100) is exactly 2.
It returns -Infinity. For a negative number it returns NaN. Neither throws an exception.
No. Math is a static class, so you call Math.Log10(...) straight on the class name. You cannot write new Math(), and there is no variable to keep.

Next: Log2()

Return the base-2 logarithm: how many times you double 1 to reach a number.

Log2() 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.

13 people found this page helpful