我正在嘗試使用 PowerShell 列印格式化的輸出,但由于某種原因,我無法理解它。數量列和成本列需要格式化,但 {1,-15:N4} 不起作用。我肯定在這里做錯了什么,但嘗試了很多其他的東西并且不起作用。
#file: daily_needs_store.ps1
#
Write-Output ("{0,29}" -f "Affordable Daily Needs")
Write-Output "This Program will prompt for 10 items."
Write-Output "Enter quantity of each item purchased."
$TaxRate1 = 8.25
[float]$breadCount = Read-Host ("{0,-50}" -f "How many loaves of Bread (`$1.75 each) ");
[float]$milkCount = Read-Host ("{0,-50}" -f "How many Gallons of Milk (`$3.29 each) ")
[float]$chipsCount = Read-Host ("{0,-50}" -f "How many Bags of Chips (`$4.19 each) ")
[float]$potatoesCount = Read-Host ("{0,-50}" -f "How many Pounds of Potatoes (`$1.49 each) ")
[float]$bananasCount = Read-Host ("{0,-50}" -f "How many Pounds of Bananas (`$0.49 each) ")
[float]$beefCount = Read-Host ("{0,-50}" -f "How many Pounds of Beef (`$5.29 each) ")
[float]$tomatoesCount = Read-Host ("{0,-50}" -f "How many Pounds of Tomatoes (`$0.99 each) ")
[float]$applesCount = Read-Host ("{0,-50}" -f "How many Pounds of Apples (`$1.29 each) ")
[float]$batteriesCount = Read-Host ("{0,-50}" -f "How many 2-packs of AA Batteries (`$2.39 each) ")
[float]$vitaminCount = Read-Host ("{0,-50}" -f "How many Bottles of Vitamin (`$4.89 each) ")
Write-Output ("{0,-50}" -f"======================================================")
Write-Output ("{0,29}" -f "Affordable Daily Needs")
Write-Output ("{0,28}" -f "4715 San Pedro Avenue")
Write-Output ("{0,29}" -f "San Antonio, TX 78217")
Write-Output ("{0,27}" -f "Phone: 210-128-1919")
Write-Output ("{0,-50}" -f"------------------------------------------------------")
[float]$total = 0.0;
$breadCost = $breadCount * 1.75
$total = $total $breadCost
$milkCost = $milkCount * 3.29
$total = $total $milkCost
$chipsCost = $chipsCount * 4.19
$total = $total $chipsCost
$potatoesCost = $potatoesCount * 1.49
$total = $total $potatoesCost
$bananasCost = $bananasCount * 0.49
$total = $total $bananasCost
$beefCost = $beefCount * 5.29
$total = $total $beefCost
$tomatoesCost = $tomatoesCount * 0.99
$total = $total $tomatoesCost
$applesCost = $applesCount * 1.29
$total = $total $applesCost
$batteriesCost = $batteriesCount * 2.39 * (1 ($TaxRate1/100))
$total = $total $batteriesCost
$vitaminCost = $vitaminCount * 4.89 * (1 ($TaxRate1/100))
$total = $total $vitaminCost
Write-Host ("{0,-10} {1,-15} {2,-25} {3,-32}" -f "Item", "Qty", "Price/Unit", "Cost" )
Write-Host ("{0,-50}" -f"------------------------------------------------------")
Write-Host ("{0,-10} {1,-15} {2,-25} {3,-32}" -f "Bread", "$breadCount", "`$1.75", "$breadCost" )
Write-Host ("{0,-10} {1,-15} {2,-25} {3,-32}" -f "Milk", "$milkCount", "`$3.29", "$milkCost" )
Write-Host ("{0,-10} {1,-15} {2,-25} {3,-32}" -f "Chips", "$chipsCount", "`$4.19", "$chipsCost" )
Write-Host ("{0,-10} {1,-15} {2,-25} {3,-32}" -f "Potatoes", "$potatoesCount", "`$1.49", "$potatoesCost" )
Write-Host ("{0,-10} {1,-15} {2,-25} {3,-32}" -f "Beef", "$beefCount", "`$5.29", "$beefCost" )
Write-Host ("{0,-10} {1,-15} {2,-25} {3,-32}" -f "Tomatoes", "$tomatoesCount", "`$0.99", "$tomatoesCost" )
Write-Host ("{0,-10} {1,-15} {2,-25} {3,-32}" -f "Apples", "$applesCount", "`$1.29", "$applesCost" )
Write-Host ("{0,-10} {1,-15} {2,-25} {3,-32}" -f "Batteries", "$batteriesCount", "`$2.39", "$batteriesCost" )
Write-Host ("{0,-10} {1,-15} {2,-25} {3,-32}" -f "Vitamins", "$vitaminCount", "`$4.89", "$vitaminCost" )
Write-Host ("{0,-50}" -f"------------------------------------------------------")
Write-Host ("{0,-10} {1,-92}" -f "Subtotal", "`$$total")
Write-Host ("{0,-10} {1,-92}" -f "Tax `@ 8.25`%", "`$$total")
我希望輸出像 輸出
謝謝
uj5u.com熱心網友回復:
這不是我們在 PowerShell 中做事的方式。您的代碼是重復的、難以閱讀且難以維護的。
我會從這樣的事情開始:
$Products = @'
Product,Price,TaxRate,Quantity
Bread,1.75,0,
Milk,3.29,0,
Chips,4.19,0,
Potatoes,1.49,0,
Batteries,2.39,8.25,
Vitamin,4.89,8.25,
'@ |
ConvertFrom-Csv
$ShoppingCart =
foreach ($item in $Products) {
[int]$Item.Quantity = Read-Host ('How many items of {0,9} (${1} each)' -f $Item.Product, $item.Price)
$item
}
$Checkout =
$ShoppingCart |
Select-Object -Property Product,Price,Quantity,
@{Name = 'SubTotal'; Expression = {[Math]::Round($([Float]$_.Price * [Int]$_.Quantity * (1 ($([Float]$_.TaxRate) / 100))),2)}}
$Total = ($Checkout | Measure-Object -Property Subtotal -Sum ).Sum
$Checkout
"`nTotal: {0}" -f $Total
輸出看起來像這樣:
Product Price Quantity SubTotal
------- ----- -------- --------
Bread 1.75 5 8,75
Milk 3.29 4 13,16
Chips 4.19 3 12,57
Potatoes 1.49 6 8,94
Batteries 2.39 2 5,17
Vitamin 4.89 8 42,35
Total: 90,94
當然還有改進的余地。;-)
順便說一句:輸出顯示逗號作為小數點,因為我的語言設定不是英語!;-)
uj5u.com熱心網友回復:
- 將虛線減少到僅 35 個字符。
- 從所有變數中洗掉雙引號,格式運算子 -f 認為它們是字串,但我們希望它知道它們實際上是數字。
- 不要在總計前放置美元符號。使用正確的格式字串。
- 在 Qty 列上使用一種型別的格式化字串,但在 Cost 列上使用不同的型別。
- 第一行使用負對齊,數字/金錢使用正對齊。
- 減少大多數對齊數字的大小,那東西太寬了!
- 底部需要 3 行,而不是 2 行。一列用于小計,然后是稅,最后是總計。
- 在適當的時間上床睡覺,每晚同一時間,每晚至少睡 7.5 小時。
uj5u.com熱心網友回復:
現在作業,我需要從變數中洗掉“”,以便 Powershell 可以將它們解釋為浮點數而不是字串。
Final: https://pastebin.com/ZjFWYM2z
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/435591.html
標籤:电源外壳
