Skip to content

Commit aa99b28

Browse files
updated examples
1 parent 7bb8c00 commit aa99b28

File tree

1 file changed

+67
-21
lines changed
  • content/c-sharp/concepts/math-functions/terms/truncate

1 file changed

+67
-21
lines changed

content/c-sharp/concepts/math-functions/terms/truncate/truncate.md

Lines changed: 67 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,65 +5,111 @@ Subjects:
55
- 'Code Foundations'
66
- 'Computer Science'
77
Tags:
8-
- 'Methods'
9-
- 'Numbers'
108
- 'Arithmetic'
119
- 'Functions'
10+
- 'Methods'
11+
- 'Numbers'
1212
CatalogContent:
1313
- 'learn-c-sharp'
1414
- 'paths/computer-science'
1515
---
1616

17-
The **`Math.Truncate()`** class method returns the integer part of a specified number by removing any fractional digits.
17+
The **`Math.Truncate()`** method in C# returns the integer part of a specified number by removing any fractional digits. It works with both `decimal` and `double` types.
1818

1919
## Syntax
2020

2121
```pseudo
22-
Math.Truncate(d);
23-
The Math.Truncate() method takes one parameter, d, which is the number to truncate (this can be a double or decimal). The method returns the integer part of d (of the same type), except if the value of d equals:
22+
Math.Truncate(double d)
23+
```
2424

25-
NaN, then it returns NaN.
25+
Or, alternatively:
2626

27-
NegativeInfinity, then it returns NegativeInfinity.
27+
```pseudo
28+
Math.Truncate(decimal d)
29+
```
2830

29-
PositiveInfinity, then it also returns PositiveInfinity.
31+
**Parameters:**
3032

31-
Note: Math.Truncate() always rounds towards zero. This means Truncate(2.8) is 2, and Truncate(-2.8) is -2. This is different from Math.Floor(), which always rounds down (e.g., Math.Floor(-2.8) would be -3).
33+
- `d` (`double` | `decimal`): The number whose fractional part is to be discarded.
3234

33-
Example
34-
The following example demonstrates the Math.Truncate() method with both a positive and a negative double. It highlights how the method rounds towards zero in both cases.
35+
**Return value:**
3536

37+
- Returns a `double` if a `double` is passed and a `decimal` if a `decimal` is passed.
38+
- Special values like `NaN`, `PositiveInfinity`, and `NegativeInfinity` are returned as-is.
39+
40+
> **Note:** `Math.Truncate()` always rounds towards zero. This means `Math.Truncate(2.8)` is 2, and `Math.Truncate(-2.8)` is -2. This is different from `Math.Floor()`, which always rounds down (e.g., `Math.Floor(-2.8)` would be -3).
41+
42+
## Example 1: Using double
43+
44+
In this example, a double value is truncated to its integer part:
45+
46+
```cs
3647
using System;
3748

3849
public class Example {
39-
public static void Main(string[] args) {
50+
public static void Main() {
4051
double positiveValue = 12.9;
4152
double negativeValue = -4.7;
4253

4354
Console.WriteLine("Truncating " + positiveValue + " gives: " + Math.Truncate(positiveValue));
4455
Console.WriteLine("Truncating " + negativeValue + " gives: " + Math.Truncate(negativeValue));
4556
}
4657
}
58+
```
4759

4860
This example results in the following output:
4961

62+
```shell
5063
Truncating 12.9 gives: 12
5164
Truncating -4.7 gives: -4
65+
```
5266

53-
Codebyte Example
54-
The following example is runnable and returns the truncated value of the given number:
67+
## Example 2: Using decimal
68+
69+
In this example, a `decimal` value is truncated without using the `m` suffix by explicitly casting a `double` to `decimal`:
70+
71+
```cs
5572
using System;
5673

5774
public class Example {
58-
public static void Main(string[] args) {
59-
// Number to truncate
60-
double number = 2.71828;
75+
public static void Main() {
76+
decimal decValue = (decimal)45.678;
77+
decimal negDecValue = (decimal)-23.456;
6178

62-
Console.WriteLine("The truncated value of " + number + " is: " + Math.Truncate(number));
79+
Console.WriteLine("Truncating decimal " + decValue + " gives: " + Math.Truncate(decValue));
80+
Console.WriteLine("Truncating decimal " + negDecValue + " gives: " + Math.Truncate(negDecValue));
81+
}
82+
}
83+
```
84+
85+
The output of this code is:
6386

64-
// Example with a negative number
65-
double negativeNumber = -3.14159;
66-
Console.WriteLine("The truncated value of " + negativeNumber + " is: " + Math.Truncate(negativeNumber));
87+
```shell
88+
Truncating decimal 45.678 gives: 45
89+
Truncating decimal -23.456 gives: -23
90+
```
91+
92+
## Codebyte Example
93+
94+
In this example, both `double` and `decimal` values are truncated in a small array of numbers:
95+
96+
```codebyte/csharp
97+
using System;
98+
99+
public class Example {
100+
public static void Main() {
101+
double[] doubles = { 3.1415, -7.89, 0.99 };
102+
decimal[] decimals = { (decimal)12.345, (decimal)-6.78 };
103+
104+
Console.WriteLine("Double values truncated:");
105+
foreach (double d in doubles) {
106+
Console.WriteLine(Math.Truncate(d));
107+
}
108+
109+
Console.WriteLine("\nDecimal values truncated:");
110+
foreach (decimal d in decimals) {
111+
Console.WriteLine(Math.Truncate(d));
112+
}
67113
}
68114
}
69115
```

0 commit comments

Comments
 (0)