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
Call
What 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
Parameter
Description
d
Required. The number whose base-10 logarithm you want. It must be greater than 0 for a normal answer.
Return Value
Return Value
Description
double
The 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));
}
}
Output
0
1
2
3
-Infinity
How It Works
1 gives 0. 10 to the power 0 is 1, so Math.Log10(1) is 0.
10 gives 1. 10 to the power 1 is 10. The answer is 1.
100 and 1000. Each extra zero adds 1 to the answer, giving 2 and 3.
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);
}
}
}
Output
5
3
4
1
How It Works
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.
Floor then add 1.Math.Log10(12345) is about 4.09. Floor gives 4, and 4 + 1 is 5 digits.
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.
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.