- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Math.Sign() Method in C#
The Math.Sign() method in C# is used to return an integer that indicates the sign of a number.
Methods
The Math.Sign() overloads the following methods −
Math.Sign(Int16) Math.Sign(Int32) Math.Sign(Int64) Math.Sign(SByte) Math.Sign(Single) Math.Sign(Decimal) Math.Sign(Double)
Example
Let us now see an example to implement Math.Sign() method −
using System; public class Demo { public static void Main(){ short val1 = 1; int val2 = 20; long val3 = -3545465777; Console.WriteLine("Short Value = " + val1); Console.WriteLine("Sign (Short Value) = " + getSign(Math.Sign(val1))); Console.WriteLine("Int32 value = " + val2); Console.WriteLine("Sign (Short Value) = " + getSign(Math.Sign(val2))); Console.WriteLine("Long value = " + val3); Console.WriteLine("Sign (Long Value) = " + getSign(Math.Sign(val3))); } public static String getSign(int compare){ if (compare == 0) return "Equal to zero!"; else if (compare < 0) return "Less than zero!"; else return "Greater than zero!"; } }
Output
This will produce the following output −
Short Value = 1 Sign (Short Value) = Greater than zero! Int32 value = 20 Sign (Short Value) = Greater than zero! Long value = -3545465777 Sign (Long Value) = Less than zero!
Example
Let us see another example to implement Math.Sign() method −
using System; public class Demo { public static void Main(){ Decimal val1 = 20m; Double val2 = -35.252d; Console.WriteLine("Decimal Value = " + val1); Console.WriteLine("Sign (Decimal Value) = " + getSign(Math.Sign(val1))); Console.WriteLine("Double value = " + val2); Console.WriteLine("Sign (Double Value) = " + getSign(Math.Sign(val2))); } public static String getSign(int compare){ if (compare == 0) return "Equal to zero!"; else if (compare < 0) return "Less than zero!"; else return "Greater than zero!"; } }
Output
This will produce the following output −
Decimal Value = 20 Sign (Decimal Value) = Greater than zero! Double value = -35.252 Sign (Double Value) = Less than zero!
Advertisements