[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[转载代码] [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

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

TOP

# 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
我帮忙写的代码不需要付钱。如果一定要给,请在微信群或QQ群发给大家吧。
【微信公众号、微信群、QQ群】http://bbs.bathome.net/thread-3473-1-1.html
【支持批处理之家,加入VIP会员!】http://bbs.bathome.net/thread-67716-1-1.html

TOP

返回列表