2
$price = 22.5

write-host "The price is $"("{0:N2}" -f ($price))"!"

#NOTE: Prints out-> The price is $ 22.50 ! (Has an EXTRA SPACE after the $ & before the !)


I can do it this long way, but it is too much work: write-host "The price is $" -nonewline write-host ("{0:N2}" -f ($price)) -nonewline write-host "!"

Olaf
  • 4,690
  • 2
  • 15
  • 23
colemd1
  • 113
  • 7
  • 4
    use >>> `write-host ('The price is ${0:N2}' -f $price)` <<< instead of the method you used. note the use of SINGLE quotes and where parens are used. [*grin*] – Lee_Dailey Mar 20 '22 at 02:28
  • 1
    `'{0:N2}' -f $price` should be all you really need – Abraham Zinala Mar 20 '22 at 03:05
  • Thanks Lee, that was exactly what I was looking for. You were informative & concise. Thanks also to Abraham, that was a nice representation of the simplest form possible. – colemd1 Mar 20 '22 at 16:50
  • Lee, I would have chose your solution as the "Correct Answer" because when you are throwing a script together and have already lost time, your type of answer is a quick relief to the frustration. – colemd1 Mar 20 '22 at 18:23

2 Answers2

3

Has an EXTRA SPACE

The reason is that you're passing three arguments to Write-Host, which the latter prints separated with spaces by default:

  • Argument 1: "The price is $"
  • Argument 2: ("{0:N2}" -f ($price))
  • Argument 3: "!"
  • Generally, note that in PowerShell you can not form a single string argument from directly concatenated quoted strings the way you can in bash, for instance, such as with "foo"'bar' - unless the first token is unquoted, PowerShell will treat the substrings as separate arguments - see this answer.

To avoid these spaces, pass a single expandable (double-quoted) string ("..."), which requires:

  • Escaping $ chars. to be used verbatim as `$ (you only got away without this in your attempt because the $ happened to be at the end of your first argument).

  • Enclosing the "{0:N2}" -f ($price) expression - which can be simplified to "{0:N2}" -f $price - in $(...), the subexpression operator

# Note: No need for Write-Host, unless you explicitly want to print
#       to the *display only*.
"The price is `$$("{0:N2}" -f $price)!"

Note:

  • Write-Host is typically the wrong tool to use, unless the intent is to write to the display only, bypassing the success output stream and with it the ability to send output to other commands, capture it in a variable, or redirect it to a file. To output a value, use it by itself; e.g., $value instead of Write-Host $value (or use Write-Output $value, though that is rarely needed); see this answer.

However, Lee Dailey shows a simpler alternative using a single expression with the -f operator, whose LHS can then be a verbatim (single-quoted) string ('...'):

# If you want to use Write-Host, enclose the entire expression in (...)
'The price is ${0:N2}!' -f $price
mklement0
  • 382,024
  • 64
  • 607
  • 775
  • Thanks mklement0, your perspective is much appreciated. My attempt to translate a simple statement from python to powershell blew up, so I started to break down the problem, ending up with 3 arguments. Interesting is your explanation on the expanded string. – colemd1 Mar 20 '22 at 17:00
  • Mklement0, it took me some time to fully appreciate your answer after which it was obvious that you had the best answer. Your info on write-host limiting output to display was an important lesson to learn, along with the expandable string. – colemd1 Mar 20 '22 at 18:27
  • I am glad to hear it, @colemd1; thanks for the nice feedback. – mklement0 Mar 20 '22 at 18:28
0

Two alternatives

  1. Using format operator -f
  2. Using string.Format Method

Code

$price = 22.5

$message = "The price is `${0:N2}!" -f $price
Write-Host $message

$message = [System.String]::Format("The price is `${0:N2}!", @($price))
Write-Host $message

Write-Host $("The price is `${0:N2}!" -f $price)
Write-Host $([System.String]::Format("The price is `${0:N2}!", @($price)))

# //Multiple parameters
$customer = "Joma"
Write-Host $("The price is `${0:N2}!. Thanks for your purchase {1}." -f $price, $customer)
Write-Host $([System.String]::Format("The price is `${0:N2}!. Thanks for your purchase {1}.", @($price, "Joma")))

Output
output

Joma
  • 3,520
  • 1
  • 29
  • 32
  • 1
    Thanks Joma. Nice set of examples with output. I will add these lessons to my knowledge base. – colemd1 Mar 20 '22 at 17:02
  • Your visual representation really hit home with me, as I am a visual learner. However, after much consideration, I had to choose Mklement0's answer as there was just so much information. In reading all of these responses, I now see why you put the formatted information to variable, so as not to limit the output to the display. Keep up your great visual answers, as they are a sure fire way to show programmers of all levels how to accomplish their goals. Please consider providing some explanation below the visual solution to clarify any key points. The links were helpful. Thanks! – colemd1 Mar 20 '22 at 18:37
  • @colemd1, I only contribute with my grain of sand. – Joma Mar 21 '22 at 03:33
  • Keep it up, the examples you provided are great! You are obviously a knowledgeable individual. – colemd1 Mar 21 '22 at 15:22