如何获得在.net中的一些的大小? [英] How to get the size of a number in .net?
问题描述
我要与给定的双号返回其大小(被称为|号码|,或数字的号码,而数字点之后),例如:
12.324654 - > 2
12 - > 2
0.99 - > 0
1.01 - > 1
901 - > 3
-0.99 - > 0
-9.999 - > 1
必须有一个功能是.NET,我不熟悉的,做它..
感谢。
( (INT)Math.Abs(12.324654))。toString()方法。长度
Math.Abs
将转换为正数(不想算负号)
(INT)
将下降小数点后面
的ToString()
转换为字符串===>12
$ b数字$ b 长度
告诉你有多少个字符有
有一个边缘情况,其中(INT)(部分号码) == 0
,其中长度是想给你你可能不希望这个1--,所以要注意的可能性
现在,你可以做的是使这是一个延伸的方法......
公共静态类utils的
{
公共静态INT大小(这双N)
{
INT I =(int)的Math.Abs(N);
如果(我== 0)返回0;
返回i.ToString()长度。
}
}
12.324654.Size()== 2;
I want with a given double number to return its "size" (known as |number|, or number of digit without the numbers after the dot), for example:
12.324654 -> 2
12 -> 2
0.99 -> 0
1.01 -> 1
901 -> 3
-0.99 -> 0
-9.999 -> 1
There must be a function is .net that I dont familiar with that does it..
Thanks.
((int)Math.Abs(12.324654)).ToString().Length
Math.Abs
will convert to positive number (don't want to count the negative sign)
(int)
will drop the digits following the decimal point
ToString()
converts to a string ===> "12"
Length
tells you how many characters there are
there is an edge case where (int)( some number ) == 0
, where the length is going to give you 1-- you may not want this, so be aware of the possibility
now, what you COULD do is make this an extention method....
public static class Utils
{
public static int Size(this double n)
{
int i = (int)Math.Abs(n);
if ( i == 0 ) return 0;
return i.ToString().Length;
}
}
12.324654.Size() == 2;
这篇关于如何获得在.net中的一些的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!