You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/c-sharp/concepts/math-functions/terms/truncate/truncate.md
+67-21Lines changed: 67 additions & 21 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,65 +5,111 @@ Subjects:
5
5
- 'Code Foundations'
6
6
- 'Computer Science'
7
7
Tags:
8
-
- 'Methods'
9
-
- 'Numbers'
10
8
- 'Arithmetic'
11
9
- 'Functions'
10
+
- 'Methods'
11
+
- 'Numbers'
12
12
CatalogContent:
13
13
- 'learn-c-sharp'
14
14
- 'paths/computer-science'
15
15
---
16
16
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.
18
18
19
19
## Syntax
20
20
21
21
```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
+
```
24
24
25
-
NaN, then it returns NaN.
25
+
Or, alternatively:
26
26
27
-
NegativeInfinity, then it returns NegativeInfinity.
27
+
```pseudo
28
+
Math.Truncate(decimal d)
29
+
```
28
30
29
-
PositiveInfinity, then it also returns PositiveInfinity.
31
+
**Parameters:**
30
32
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.
32
34
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:**
35
36
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:
0 commit comments