|
| 1 | +--- |
| 2 | +Title: '.Sign()' |
| 3 | +Description: 'Returns an integer value indicating the sign of a number.' |
| 4 | +Subjects: |
| 5 | + - 'Code Foundations' |
| 6 | + - 'Computer Science' |
| 7 | +Tags: |
| 8 | + - 'Functions' |
| 9 | + - 'Math' |
| 10 | + - 'Methods' |
| 11 | + - 'Numbers' |
| 12 | +CatalogContent: |
| 13 | + - 'learn-c-sharp' |
| 14 | + - 'paths/computer-science' |
| 15 | +--- |
| 16 | + |
| 17 | +The **`Math.Sign()`** method is a static method that returns an integer value indicating the sign of a specified number. |
| 18 | + |
| 19 | +## Syntax |
| 20 | + |
| 21 | +```pseudo |
| 22 | +Math.Sign(number); |
| 23 | +``` |
| 24 | + |
| 25 | +**Parameters:** |
| 26 | + |
| 27 | +- `number`: The numeric value for which to determine the sign. Supported types include: `sbyte`, `short`, `int`, `long`, `float`, `double`, and `decimal`. |
| 28 | + |
| 29 | +**Return value:** |
| 30 | + |
| 31 | +Returns an integer value that indicates the sign of the specified number: |
| 32 | + |
| 33 | +- `-1`: if the number is negative |
| 34 | +- `0`: if the number is equal to zero |
| 35 | +- `1`: if the number is positive |
| 36 | + |
| 37 | +## Example: Basic Usage of `Math.Sign()` |
| 38 | + |
| 39 | +In this example, the sign of various numeric values is determined using the `Math.Sign()` method and printed to the console: |
| 40 | + |
| 41 | +```cs |
| 42 | +using System; |
| 43 | + |
| 44 | +public class Example { |
| 45 | + public static void Main () { |
| 46 | + // Example numeric values |
| 47 | + int numInt = -50; |
| 48 | + float numFloat = 0.0f; |
| 49 | + double numDouble = 2.7; |
| 50 | + decimal numDecimal = -0.01m; |
| 51 | + |
| 52 | + // Determine the sign of each number |
| 53 | + int numIntSign = Math.Sign(numInt); |
| 54 | + int numFloatSign = Math.Sign(numFloat); |
| 55 | + int numDoubleSign = Math.Sign(numDouble); |
| 56 | + int numDecimalSign = Math.Sign(numDecimal); |
| 57 | + |
| 58 | + // Print the results |
| 59 | + Console.WriteLine($"Math.Sign({numInt}) = {numIntSign}"); |
| 60 | + Console.WriteLine($"Math.Sign({numFloat}) = {numFloatSign}"); |
| 61 | + Console.WriteLine($"Math.Sign({numDouble}) = {numDoubleSign}"); |
| 62 | + Console.WriteLine($"Math.Sign({numDecimal}) = {numDecimalSign}"); |
| 63 | + } |
| 64 | +} |
| 65 | +``` |
| 66 | + |
| 67 | +This example outputs the following: |
| 68 | + |
| 69 | +```shell |
| 70 | +Math.Sign(-50) = -1 |
| 71 | +Math.Sign(0) = 0 |
| 72 | +Math.Sign(2.7) = 1 |
| 73 | +Math.Sign(-0.01) = -1 |
| 74 | +``` |
| 75 | + |
| 76 | +## Codebyte Example |
| 77 | + |
| 78 | +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: |
| 79 | + |
| 80 | +```codebyte/csharp |
| 81 | +using System; |
| 82 | +
|
| 83 | +public class Example { |
| 84 | + public static void Main () { |
| 85 | + // Initialize random number generator |
| 86 | + var randNumGenerator = new Random(); |
| 87 | +
|
| 88 | + // Generator random number between -100 and 100 |
| 89 | + int randomNum = randNumGenerator.Next(-100, 100); |
| 90 | +
|
| 91 | + // Determine the sign of randomNum |
| 92 | + int randomNumSign = Math.Sign(randomNum); |
| 93 | +
|
| 94 | + // Print message based on sign |
| 95 | + if (randomNumSign == -1) { |
| 96 | + Console.WriteLine($"Random number, {randomNum}, is negative."); |
| 97 | + } else if (randomNumSign == 1) { |
| 98 | + Console.WriteLine($"Random number, {randomNum}, is positive."); |
| 99 | + } else { |
| 100 | + Console.WriteLine($"Random number, {randomNum}, is zero."); |
| 101 | + } |
| 102 | + } |
| 103 | +} |
| 104 | +``` |
0 commit comments