PowerShell Round&将Float格式设置为最多2个小数? [英] PowerShell Round & Format Float to max 2 decimals?
本文介绍了PowerShell Round&将Float格式设置为最多2个小数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我发现很多东西可以将浮点数格式化为众所周知的数字,但是我如何才能将浮点数的格式设置为最多2个小数,但仅在需要小数的情况下?
I found lots of stuff to format floats to common known numbers, but how can I format a float to a max of 2 decimals, but only if the decimals are needed?
示例:
1.11 # not 1.111
1.12 # it was 1.116 (round up)
1.1 # not 1.10
1 # not 1.00
如果我愿意
$('{0:N2}' -f $flt)
我知道
1.00 # :(
提前谢谢!
推荐答案
使用 [math] :: round
,即:
[math]::round(1.111,2)
将返回 1.11
和
[math]::round(1.00,2)
产量 1
这篇关于PowerShell Round&将Float格式设置为最多2个小数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文