批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程
[批处理文件精品]批处理版照片整理器[批处理文件精品]纯批处理备份&还原驱动在线第三方下载
返回列表 发帖

[转载代码] [PowerShell每日技巧]转换数字格式(20140305)

Often, users need to format numbers and limit the number of digits, or add leading zeros. There is one simple and uniform strategy for this: the operator "-f"!

Let's make sure a number has leading zeros:

PS C:\> $number = 68
PS C:\> '{0:d7}' -f $number
0000068


This will produce a 7-digit number with leading zeros. Adjust the number after "d" to control the number of digits.

To limit the number of digits, use "n" instead of "d". This time, the number after "n" controls the number of digits:

PS C:\> $number = 35553568.67826738
PS C:\> '{0:n1}' -f $number
35,553,568.7


Likewise, use "p" to format percentages:

PS C:\> $number = 0.32562176536
PS C:\> '{0:p2}' -f $number
32.56 %


http://powershell.com/cs/blogs/tips/archive/2014/03/05/formatting-numbers-easily.aspx

# Dec to Hex
PS C:\> $number = 255
PS C:\> '{0:X0}' -f $number
FF

# Thousands Separators
PS C:\> $number = 1234567
PS C:\> '{0:n0}' -f $number
1,234,567

# Currency
PS C:\> $number = 35553568.67826738
PS C:\> '{0:c1}' -f $number
$35,553,568.7
【批处理在线视频分享】http://bbs.bathome.net/thread-31727-1-1.html
【微信公众号、微信群、QQ群】http://bbs.bathome.net/thread-3473-1-1.html

我帮忙写的代码不需要付钱,不用找我要支付宝或微信账号。如果一定要给,请到微信群给大家发红吧。

TOP

左侧补零的另外一种方式:
PS C:\> $number = '68'
PS C:\> $number.PadLeft(7, '0')
0000068

TOP

返回列表