6

I'm trying to print formatted output using PowerShell but for some reason, I can't wrap my head around it. Quantity column and Cost column need formatting but {1,-15:N4} doesn't work. I'm definitely doing something wrong here but tried a bunch of other stuff and doesn't work.

#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") 

I want the output to be like this:

output

1
  • To benefit future readers, can you improve the description of "doesn't work" in this post? "Doesn't work" isn't an accurate fault report. In general it is better to say what actually happens and why this is not what you expected. Commented Mar 2, 2022 at 8:48

3 Answers 3

6

That's not how we do things in PowerShell. Your code is repetitive, hard to read and hard to maintain.

I'd start with something like this:

$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

The output would look something like this:

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

Of course there's room for improvement. ;-)

BTW: The output shows a comma as the decimal point because my language setting is not english! ;-)

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for taking the time to answer :) This seems a bit advanced for me but yours look definitely better :)
1

Working now, I needed to remove "" from variables so Powershell can interpret them as floats instead of a string.

#file: daily_needs_store.ps1
#
Write-Host ("`n{0,29}" -f "Affordable Daily Needs")
Write-Host "This Program will prompt for 10 items."
Write-Host "Enter quantity of each item purchased."

$TaxRate = 8.25

[float]$breadCount = Read-Host ("{0,-45}" -f "How many loaves of Bread (`$1.75 each)")
[float]$milkCount = Read-Host ("{0,-45}" -f "How many Gallons of Milk (`$3.29 each)")
[float]$chipsCount = Read-Host ("{0,-45}" -f "How many Bags of Chips (`$4.19 each)")
[float]$potatoesCount = Read-Host ("{0,-45}" -f "How many Pounds of Potatoes (`$1.49 each)")
[float]$bananasCount = Read-Host ("{0,-45}" -f "How many Pounds of Bananas (`$0.49 each)")
[float]$beefCount = Read-Host ("{0,-45}" -f "How many Pounds of Beef (`$5.29 each)   ")
[float]$tomatoesCount = Read-Host ("{0,-45}" -f "How many Pounds of Tomatoes (`$0.99 each)")
[float]$applesCount = Read-Host ("{0,-45}" -f "How many Pounds of Apples (`$1.29 each)")
[float]$batteriesCount = Read-Host ("{0,-45}" -f "How many 2-packs of AA Batteries (`$2.39 each)")
[float]$vitaminCount = Read-Host ("{0,-45}" -f "How many Bottles of Vitamin (`$4.89 each)")

Write-Host ("{0,-50}" -f "===================================")
Write-Host ("{0,29}" -f "Affordable Daily Needs")
Write-Host ("{0,28}" -f "4715 San Pedro Avenue")
Write-Host ("{0,29}" -f "San Antonio, TX 78217")
Write-Host ("{0,27}" -f "Phone: 210-128-1919")
Write-Host ("{0,-50}" -f "-----------------------------------")

[float]$total = 0.0;
[float]$totalTax = 0.0;
[float]$subTotal = 0.0;

$breadCost = $breadCount * 1.75
$subTotal = $subTotal + $breadCost

$milkCost = $milkCount * 3.29
$subTotal = $subTotal + $milkCost

$chipsCost = $chipsCount * 4.19
$subTotal = $subTotal + $chipsCost

$potatoesCost = $potatoesCount * 1.49
$subTotal = $subTotal + $potatoesCost

$bananasCost = $bananasCount * 0.49
$subTotal = $subTotal + $bananasCost

$beefCost = $beefCount * 5.29
$subTotal = $subTotal + $beefCost

$tomatoesCost = $tomatoesCount * 0.99
$subTotal = $subTotal + $tomatoesCost

$applesCost = $applesCount * 1.29
$subTotal = $subTotal + $applesCost

$batteriesCost = $batteriesCount * 2.39
$subTotal = $subTotal + $batteriesCost
$totalTax = $totalTax + ($batteriesCost * (1 + ($TaxRate/100)) - $batteriesCost)

$vitaminCost = $vitaminCount * 4.89 
$subTotal = $subTotal + $vitaminCost
$totalTax = $totalTax + ($vitaminCost * (1 + ($TaxRate/100)) - $vitaminCost)

$total = $subTotal + $totalTax


Write-Host ("{0,-10} {1,4} {2,11} {3,6}" -f "Item", "Qty", "Price/Unit", "Cost" )
Write-Host ("{0,-50}" -f  "-----------------------------------")
Write-Host ("{0,-10} {1,4:N1} {2,9} {3,8:N2}" -f "Bread", $breadCount, "`$1.75", $breadCost )
Write-Host ("{0,-10} {1,4:N1} {2,9} {3,8:N2}" -f "Milk", $milkCount, "`$3.29", $milkCost )
Write-Host ("{0,-10} {1,4:N1} {2,9} {3,8:N2}" -f "Chips", $chipsCount, "`$4.19", $chipsCost )
Write-Host ("{0,-10} {1,4:N1} {2,9} {3,8:N2}" -f "Potatoes", $potatoesCount, "`$1.49", $potatoesCost )
Write-Host ("{0,-10} {1,4:N1} {2,9} {3,8:N2}" -f "Beef", $beefCount, "`$5.29", $beefCost )
Write-Host ("{0,-10} {1,4:N1} {2,9} {3,8:N2}" -f "Tomatoes", $tomatoesCount, "`$0.99", $tomatoesCost )
Write-Host ("{0,-10} {1,4:N1} {2,9} {3,8:N2}" -f "Apples", $applesCount, "`$1.29", $applesCost )
Write-Host ("{0,-10} {1,4:N1} {2,9} {3,8:N2}" -f "Batteries", $batteriesCount, "`$2.39", $batteriesCost )
Write-Host ("{0,-10} {1,4:N1} {2,9} {3,8:N2}" -f "Vitamins", $vitaminCount, "`$4.89", $vitaminCost )
Write-Host ("{0,-50}" -f "-----------------------------------")
Write-Host ("{0,-10} {1,23:c2}" -f "Subtotal", $subTotal) 
Write-Host ("{0,-10} {1,22:c2}" -f "Tax `@ 8.25`%", $totalTax) 
Write-Host ("{0,-50}" -f "-----------------------------------")
Write-Host ("{0,-10} {1,23:c2}" -f "Total", $total) 
Write-Host ("{0,-50}" -f"===================================")
Write-Host "Sales Clerk: John Doe"
Write-Host "Counter#: 7"
Write-Host "Date:"(Get-Date)

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
0
  1. Reduce dashed lines to just 35 characters.
  2. REMOVE double quotes from all variables, the format operator -f thinks they are strings, but we want it to know that they are actually numbers.
  3. Do NOT place a dollar sign in front of total. Use the correct formatting strings.
  4. Use one type of formatting string on Qty column, but a different type for Cost column.
  5. Use negative alignment on first row, positive alignment for numbers/money.
  6. Reduce the size of most alignment numbers, that thing way too wide!
  7. You need 3 rows at the bottom, not 2. One for Subtotal, then Tax, and finally Total.
  8. Go to bed at decent hour, same time each night, and get at least 7.5 hours of sleep per night.

3 Comments

Thank you so much! Applied 1 through 7. It's good for submission right now. When I pasted the above code I was so focused on how to fix the precision but ( 2. ) helped me understand my error :D I do sleep good but thank you! :) Here is the finished code : pastebin.com/ZjFWYM2z
Glad it helped. I tried it as is and I couldn't get the formatting strings to do anything. Finally realized -f's problem as described in (2.).
Yes, I googled that problem for probably at least an hour, Instructor showed us bash before and he started Powershell now so I was really confused lol. Also, I read the number 8 again, I change the time I go to bed almost every day. I get to sleep around 7.5-8 hours but do you think bedtime needs to be constant?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.