From e9322761687f382066548cff52f06696c43757af Mon Sep 17 00:00:00 2001 From: Kate Suraev Date: Sun, 9 Nov 2025 18:09:11 +1100 Subject: [PATCH 1/3] [Term Entry] C# Math-functions: Sign() --- .../math-functions/terms/sign/sign.md | 104 ++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 content/c-sharp/concepts/math-functions/terms/sign/sign.md diff --git a/content/c-sharp/concepts/math-functions/terms/sign/sign.md b/content/c-sharp/concepts/math-functions/terms/sign/sign.md new file mode 100644 index 00000000000..65401be2757 --- /dev/null +++ b/content/c-sharp/concepts/math-functions/terms/sign/sign.md @@ -0,0 +1,104 @@ +--- +Title: '.Sign()' +Description: 'Returns an integer value indicating the sign of a number.' +Subjects: + - 'Computer Science' + - 'Code Foundations' +Tags: + - 'Functions' + - 'Math' + - 'Numbers' + - 'Methods' +CatalogContent: + - 'learn-c-sharp' + - 'paths/computer-science' +--- + +The **`Math.Sign()`** method is a static method that returns an integer value indicating the sign of a specified number. + +## Syntax + +```pseudo +Math.Sign(number); +``` + +**Parameters:** + +- `number`: The numeric value for which to determine the sign. This can be of type: `sbyte`, `short`, `int`, `long`, `float`, `double`, `decimal` or `IntPtr`. + +**Return value:** + +Returns an integer value that indicates the sign of the specified number: + +- `-1`: if the number is negative +- `0`: if the number is equal to zero +- `1`: if the number is positive + +## Example: Basic Usage + +In this example, the sign of various numeric values is determined using the `Math.Sign()` method and printed to the console. + +```cs +using System; + +public class Example { + public static void Main () { + // Example numeric values + int numInt = -50; + float numFloat = 0.0f; + double numDouble = 2.7; + decimal numDecimal = -0.01m; + + // Determine the sign of each number + int numIntSign = Math.Sign(numInt); + int numFloatSign = Math.Sign(numFloat); + int numDoubleSign = Math.Sign(numDouble); + int numDecimalSign = Math.Sign(numDecimal); + + // Print the results + Console.WriteLine($"Math.Sign({numInt}) = {numIntSign}"); + Console.WriteLine($"Math.Sign({numFloat}) = {numFloatSign}"); + Console.WriteLine($"Math.Sign({numDouble}) = {numDoubleSign}"); + Console.WriteLine($"Math.Sign({numDecimal}) = {numDecimalSign}"); + } +} +``` + +This example outputs the following: + +```shell +Math.Sign(-50) = -1 +Math.Sign(0) = 0 +Math.Sign(2.7) = 1 +Math.Sign(-0.01) = -1 +``` + +## Codebyte Example + +In this example, a random number between -100 and 100 is generated, and its sign is determined using the `Math.Sign()` method. A message is printed to indicate whether the random number is negative, positive, or zero. + +```codebyte/csharp +using System; + +public class Example { + public static void Main () { + // Initialize random number generator + var randNumGenerator = new Random(); + + // Generator random number between -100 and 100 + int randomNum = randNumGenerator.Next(-100, 100); + + // Determine the sign of randomNum + int randomNumSign = Math.Sign(randomNum); + + // Print message based on sign + if (randomNumSign == -1) { + Console.WriteLine($"Random number, {randomNum}, is negative."); + } else if (randomNumSign == 1) { + Console.WriteLine($"Random number, {randomNum}, is positive."); + } else { + Console.WriteLine($"Random number, {randomNum}, is zero."); + } + } +} +``` From e1c7765863f01e986e4ed42e16a37399bb8d94ec Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Mon, 10 Nov 2025 12:36:52 +0530 Subject: [PATCH 2/3] minor content fixes --- .../c-sharp/concepts/math-functions/terms/sign/sign.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/content/c-sharp/concepts/math-functions/terms/sign/sign.md b/content/c-sharp/concepts/math-functions/terms/sign/sign.md index 65401be2757..c32cd8c87e9 100644 --- a/content/c-sharp/concepts/math-functions/terms/sign/sign.md +++ b/content/c-sharp/concepts/math-functions/terms/sign/sign.md @@ -7,8 +7,8 @@ Subjects: Tags: - 'Functions' - 'Math' - - 'Numbers' - 'Methods' + - 'Numbers' CatalogContent: - 'learn-c-sharp' - 'paths/computer-science' @@ -24,7 +24,7 @@ Math.Sign(number); **Parameters:** -- `number`: The numeric value for which to determine the sign. This can be of type: `sbyte`, `short`, `int`, `long`, `float`, `double`, `decimal` or `IntPtr`. +- `number`: The numeric value for which to determine the sign. Supported types include: `sbyte`, `short`, `int`, `long`, `float`, `double`, and `decimal`. **Return value:** @@ -34,9 +34,9 @@ Returns an integer value that indicates the sign of the specified number: - `0`: if the number is equal to zero - `1`: if the number is positive -## Example: Basic Usage +## Example: Basic Usage of `Math.Sign()` -In this example, the sign of various numeric values is determined using the `Math.Sign()` method and printed to the console. +In this example, the sign of various numeric values is determined using the `Math.Sign()` method and printed to the console: ```cs using System; @@ -75,7 +75,7 @@ Math.Sign(-0.01) = -1 ## Codebyte Example -In this example, a random number between -100 and 100 is generated, and its sign is determined using the `Math.Sign()` method. A message is printed to indicate whether the random number is negative, positive, or zero. +In this example, a random number between -100 and 100 is generated, and its sign is determined using the `Math.Sign()` method. A message is printed to indicate whether the random number is negative, positive, or zero: ```codebyte/csharp using System; From 267e2206c6877d63f40e229cffd40a0abdfead70 Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Mon, 10 Nov 2025 12:37:24 +0530 Subject: [PATCH 3/3] gave alphabetical order for subjects --- content/c-sharp/concepts/math-functions/terms/sign/sign.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/c-sharp/concepts/math-functions/terms/sign/sign.md b/content/c-sharp/concepts/math-functions/terms/sign/sign.md index c32cd8c87e9..35a76e791c0 100644 --- a/content/c-sharp/concepts/math-functions/terms/sign/sign.md +++ b/content/c-sharp/concepts/math-functions/terms/sign/sign.md @@ -2,8 +2,8 @@ Title: '.Sign()' Description: 'Returns an integer value indicating the sign of a number.' Subjects: - - 'Computer Science' - 'Code Foundations' + - 'Computer Science' Tags: - 'Functions' - 'Math'