Powershell学习笔记2-数字

Manning-Powershell in Action
Page 64
 
Numbers and numeric literals
As mentioned earlier, Powershell supports all the basic .NET numeric types and performs conversions to and from the different types as needed.Table 3.2 lists these numeric types.
正如先前所提到的,Powershell支持所有的.NET基础数字类型,如果需要的话可以和其他不同种类进行转换。表3.2列出了这些数字种类
Now that we know the basic numeric types, we need to understand how are literals of
each type are specified.
现在我们知道了基础的数字种类,我们需要明白每一种是如何区分的。
 
Specifying numeric literals
In general, you don’t specify a literal having a particular type; the system will figure out the best way to represent the number. By default, an integer will be used. If the literal is too large for a 32-bit integer, a 64-bit integer will be used instead. If it’s still too big or if it contains a decimal point, a System.Double will be used. (System.Single is usually skipped, but it offers no advantages and just complicates the process.) The one case where you do want to tell the system that you’re requesting a specific type is with the System.Decimal type. These are specified by placing a “d”
at the end of the number with no intervening space, as shown:
一般情况下,你不需要为某个字符决定他的类型,系统会自动挑选最合适的。默认情况下,先尝试赋予整型,如果数超过了32比特的整数,则使用64比特整型替代。如果仍然不够大的话,或者说有一个小数点,则判断为双精度型(通常跳过单精度,因为没有优点而且使得过程变得复杂)。你需要告知系统你使用的类型的是小数型。这种类型在数的末位添加一个小写的d,没有空格,如下所示:
PS C:\Documents and Settings\shaopeng_xie> (12).gettype().fullname
System.Int32
PS C:\Documents and Settings\shaopeng_xie> (12d).gettype().fullname
System.Decimal
PS C:\Documents and Settings\shaopeng_xie> (12.34).gettype().fullname
System.Double
PS C:\Documents and Settings\shaopeng_xie> (12.34d).gettype().fullname
System.Decimal
You can see that in each case where there is a trailing “d”, the literal results in a [decimal] value being created. (If there is a space between the number and the “d”,
you’ll just get an error.)
你可以发现如果数的末尾包含d的,则判断为decimal,如果在数和d之间有空格,则会导致错误。
 
The multiplier suffixes
Of course, plain numbers are fine for most applications, but in the system administration world, there are many special values that you want to be able to conveniently represent, namely those powers of two―kilobytes, megabytes, and gigabytes. Power-Shell provides a set of multiplier suffixes for common sizes to help with this, as listed in table 3.3.
当然,对大多数程序而言普通的数字已经很好了,但在系统管理员的世界里,有许多特殊的值你想方便地表示,比如KB,MB和GB。Poweshell提供了一套乘数后缀词来解决这个问题,列在表3.3中
Hexadecimal literals
十六进制数
The last item we cover in this section is hexadecimal literals. When working with computers, it’s obviously useful to be able to specify hex literals. PowerShell uses the same notation as C, C#, and so on; namely preceding the number with the sequence “0x” and allowing the letters A-F as the extra digits. As always, the notation is caseinsensitive as shown in the following examples.
这节里我们最后涉及的话题就是十六进制数。在电脑里能够分辨十六进制数是十分有用的。Powershell使用了C、C#相同的注释方法,就是在数字前加上“0x”用A到F表示9以外的数值。例如:
PS C:\Documents and Settings\shaopeng_xie> 0x10
16
PS C:\Documents and Settings\shaopeng_xie> 0x55
85
PS C:\Documents and Settings\shaopeng_xie> 0x123456789abcdef
81985529216486895
PS C:\Documents and Settings\shaopeng_xie> 0xdeadbeef
-559038737
 

本文出自 “xmuxsp” 博客,谢绝转载!

你可能感兴趣的:(职场,powershell,休闲)