From 7bb8c00693a246e93df5fa2cd33da9af413f1161 Mon Sep 17 00:00:00 2001 From: SPBasu-win Date: Thu, 6 Nov 2025 17:59:56 +0530 Subject: [PATCH 1/3] feat: Add C# Math.Truncate() entry --- .../math-functions/terms/truncate/truncate.md | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 content/c-sharp/concepts/math-functions/terms/truncate/truncate.md diff --git a/content/c-sharp/concepts/math-functions/terms/truncate/truncate.md b/content/c-sharp/concepts/math-functions/terms/truncate/truncate.md new file mode 100644 index 00000000000..b112e277672 --- /dev/null +++ b/content/c-sharp/concepts/math-functions/terms/truncate/truncate.md @@ -0,0 +1,69 @@ +--- +Title: '.Truncate()' +Description: 'Returns the integer part of a specified number by removing any fractional digits.' +Subjects: + - 'Code Foundations' + - 'Computer Science' +Tags: + - 'Methods' + - 'Numbers' + - 'Arithmetic' + - 'Functions' +CatalogContent: + - 'learn-c-sharp' + - 'paths/computer-science' +--- + +The **`Math.Truncate()`** class method returns the integer part of a specified number by removing any fractional digits. + +## Syntax + +```pseudo +Math.Truncate(d); +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: + +NaN, then it returns NaN. + +NegativeInfinity, then it returns NegativeInfinity. + +PositiveInfinity, then it also returns PositiveInfinity. + +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). + +Example +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. + +using System; + +public class Example { + public static void Main(string[] args) { + double positiveValue = 12.9; + double negativeValue = -4.7; + + Console.WriteLine("Truncating " + positiveValue + " gives: " + Math.Truncate(positiveValue)); + Console.WriteLine("Truncating " + negativeValue + " gives: " + Math.Truncate(negativeValue)); + } +} + +This example results in the following output: + +Truncating 12.9 gives: 12 +Truncating -4.7 gives: -4 + +Codebyte Example +The following example is runnable and returns the truncated value of the given number: +using System; + +public class Example { + public static void Main(string[] args) { + // Number to truncate + double number = 2.71828; + + Console.WriteLine("The truncated value of " + number + " is: " + Math.Truncate(number)); + + // Example with a negative number + double negativeNumber = -3.14159; + Console.WriteLine("The truncated value of " + negativeNumber + " is: " + Math.Truncate(negativeNumber)); + } +} +``` From aa99b287338ead29d0017cedc62b9f38fc6d6dee Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Fri, 7 Nov 2025 12:06:10 +0530 Subject: [PATCH 2/3] updated examples --- .../math-functions/terms/truncate/truncate.md | 88 ++++++++++++++----- 1 file changed, 67 insertions(+), 21 deletions(-) diff --git a/content/c-sharp/concepts/math-functions/terms/truncate/truncate.md b/content/c-sharp/concepts/math-functions/terms/truncate/truncate.md index b112e277672..858abbd2cb2 100644 --- a/content/c-sharp/concepts/math-functions/terms/truncate/truncate.md +++ b/content/c-sharp/concepts/math-functions/terms/truncate/truncate.md @@ -5,38 +5,49 @@ Subjects: - 'Code Foundations' - 'Computer Science' Tags: - - 'Methods' - - 'Numbers' - 'Arithmetic' - 'Functions' + - 'Methods' + - 'Numbers' CatalogContent: - 'learn-c-sharp' - 'paths/computer-science' --- -The **`Math.Truncate()`** class method returns the integer part of a specified number by removing any fractional digits. +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. ## Syntax ```pseudo -Math.Truncate(d); -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: +Math.Truncate(double d) +``` -NaN, then it returns NaN. +Or, alternatively: -NegativeInfinity, then it returns NegativeInfinity. +```pseudo +Math.Truncate(decimal d) +``` -PositiveInfinity, then it also returns PositiveInfinity. +**Parameters:** -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). +- `d` (`double` | `decimal`): The number whose fractional part is to be discarded. -Example -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. +**Return value:** +- Returns a `double` if a `double` is passed and a `decimal` if a `decimal` is passed. +- Special values like `NaN`, `PositiveInfinity`, and `NegativeInfinity` are returned as-is. + +> **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). + +## Example 1: Using double + +In this example, a double value is truncated to its integer part: + +```cs using System; public class Example { - public static void Main(string[] args) { + public static void Main() { double positiveValue = 12.9; double negativeValue = -4.7; @@ -44,26 +55,61 @@ public class Example { Console.WriteLine("Truncating " + negativeValue + " gives: " + Math.Truncate(negativeValue)); } } +``` This example results in the following output: +```shell Truncating 12.9 gives: 12 Truncating -4.7 gives: -4 +``` -Codebyte Example -The following example is runnable and returns the truncated value of the given number: +## Example 2: Using decimal + +In this example, a `decimal` value is truncated without using the `m` suffix by explicitly casting a `double` to `decimal`: + +```cs using System; public class Example { - public static void Main(string[] args) { - // Number to truncate - double number = 2.71828; + public static void Main() { + decimal decValue = (decimal)45.678; + decimal negDecValue = (decimal)-23.456; - Console.WriteLine("The truncated value of " + number + " is: " + Math.Truncate(number)); + Console.WriteLine("Truncating decimal " + decValue + " gives: " + Math.Truncate(decValue)); + Console.WriteLine("Truncating decimal " + negDecValue + " gives: " + Math.Truncate(negDecValue)); + } +} +``` + +The output of this code is: - // Example with a negative number - double negativeNumber = -3.14159; - Console.WriteLine("The truncated value of " + negativeNumber + " is: " + Math.Truncate(negativeNumber)); +```shell +Truncating decimal 45.678 gives: 45 +Truncating decimal -23.456 gives: -23 +``` + +## Codebyte Example + +In this example, both `double` and `decimal` values are truncated in a small array of numbers: + +```codebyte/csharp +using System; + +public class Example { + public static void Main() { + double[] doubles = { 3.1415, -7.89, 0.99 }; + decimal[] decimals = { (decimal)12.345, (decimal)-6.78 }; + + Console.WriteLine("Double values truncated:"); + foreach (double d in doubles) { + Console.WriteLine(Math.Truncate(d)); + } + + Console.WriteLine("\nDecimal values truncated:"); + foreach (decimal d in decimals) { + Console.WriteLine(Math.Truncate(d)); + } } } ``` From 4717c493b810421f872525f7d099288572ed00e0 Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Fri, 7 Nov 2025 12:07:03 +0530 Subject: [PATCH 3/3] Update truncate.md --- .../concepts/math-functions/terms/truncate/truncate.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/content/c-sharp/concepts/math-functions/terms/truncate/truncate.md b/content/c-sharp/concepts/math-functions/terms/truncate/truncate.md index 858abbd2cb2..4b802654ebf 100644 --- a/content/c-sharp/concepts/math-functions/terms/truncate/truncate.md +++ b/content/c-sharp/concepts/math-functions/terms/truncate/truncate.md @@ -39,7 +39,7 @@ Math.Truncate(decimal d) > **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). -## Example 1: Using double +## Example 1: Using `double` In this example, a double value is truncated to its integer part: @@ -64,7 +64,7 @@ Truncating 12.9 gives: 12 Truncating -4.7 gives: -4 ``` -## Example 2: Using decimal +## Example 2: Using `decimal` In this example, a `decimal` value is truncated without using the `m` suffix by explicitly casting a `double` to `decimal`: