返回符号的功能 [英] A function to return a sign

查看:55
本文介绍了返回符号的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这真的很简单,但我的大脑却无法正常工作.我需要C#中的一个函数,如果传递给该函数的整数具有负号,则返回-1,如果传递给该整数的正号,则返回1,如果传递的数字为0,则返回0.例如:

I know this will really turn out to be simple, but my brain is just not working. I need a function in C# that will return -1 if the integer passed to the function has a negative sign, return 1 if the integer has a positive sign and return 0 if the number passed is 0. So for example:

int Sign=SignFunction(-82); // Should return -1
int Sign2=SignFunction(197); // Should return 1
int Sign3=SignFunction(0);   // Should return 0

推荐答案

这已经在框架中.只需使用 Math.Sign ...

This is already in the framework. Just use Math.Sign...

int sign = Math.Sign(-82); // Should return -1
int sign2 = Math.Sign(197); // Should return 1
int sign3 = Math.Sign(0);   // Should return 0

此外,它还可以与:

int sign4 = Math.Sign(-5.2); // Double value, returns -1
int sign5 = Math.Sign(0.0m); // Decimal, returns 0
// ....

这篇关于返回符号的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆