none
Formatting Variable into Currency Format INSIDE script RRS feed

  • Question

  • Hello, 

    I was wondering if someone could give me insight on why I'm running into some problems.

    So, in my script, I'm trying to display my [decimal]$Variable into currency format ( $x.xx ) 

    By looking around online, I found two different ways;

    "{0:C2}" -f $Variable will create the appropriate output in my open shell

    $Variable.ToString("c") will also work in my shell. 

    Now my only problem is that despite numerous attempts and quadrouple checking that I typed it into the ISE properly, I can't for the life of me get it to display properly in my script. 

    I even just tried saving the output into a variable before I put it into my write-host line.

    Does anyone know what the problem is? 

    Thanks, Phil

    Tuesday, October 14, 2014 10:22 PM

Answers

  • This is easier if you use string formatting and the -f operator. Example:


    $TaxPayable = 33.25
    $TotalCost = 124.21
    "The tax payable is {0:C}, and total cost is {1:C}." -f $TaxPayable,$TotalCost
    


    -- Bill Stewart [Bill_Stewart]

    Tuesday, October 14, 2014 10:36 PM
    Moderator

All replies

  • Keep in mind that we can't see your screen.

    Post a short code sample that contains only the minimum amount of code needed to reproduce the problem. Also explain what output you want and the output you're getting instead.


    -- Bill Stewart [Bill_Stewart]

    Tuesday, October 14, 2014 10:27 PM
    Moderator
  • $TaxPayable.ToString("c") | Set-Variable -Name $TaxInCurrency 
    $TotalCost.ToString("c") | Set-Variable -Name $TotalCostInCurrency

    ##########################Display the $TaxPayable and $TotalCost to the user###################################
      Write-Host "The Tax for the Book is $TaxInCurrency, making the total cost $TotalCostInCurrency"

    This is my latest attempt to make it work. Sorry about the initial lack of information

     
    Tuesday, October 14, 2014 10:29 PM
  • This is easier if you use string formatting and the -f operator. Example:


    $TaxPayable = 33.25
    $TotalCost = 124.21
    "The tax payable is {0:C}, and total cost is {1:C}." -f $TaxPayable,$TotalCost
    


    -- Bill Stewart [Bill_Stewart]

    Tuesday, October 14, 2014 10:36 PM
    Moderator
  • Thank you so much ! 

      I just wanted to mention that this only worked when the line was typed as it. When I had Write-Host before the line the script skipped it for some reason. 

    Tuesday, October 14, 2014 10:44 PM
  • Write-Host ('The tax payable is {0:C}, and total cost is {1:C}.' -f $TaxPayable,$TotalCost) -fore green

    ¯\_(ツ)_/¯

    Tuesday, October 14, 2014 11:34 PM
  • This too:

    $msg='The tax payable is {0:C}, and total cost is {1:C}.' -f $TaxPayable,$TotalCost
    Write-Host $msg -fore green


    ¯\_(ツ)_/¯

    Tuesday, October 14, 2014 11:36 PM
  • Don't use Write-Host if you intend to redirect the output somewhere. (Write-Host's output cannot be redirected.)

    -- Bill Stewart [Bill_Stewart]

    Tuesday, October 14, 2014 11:42 PM
    Moderator
  • Bill is noting that Write-Host was designed to send operator messages and not to produce persisted output.  YOU can send Write-Host messages and send output.  In the pipeline the output is passed through the pipeline but Write-Host is displayed on the console as it is executed.


    ¯\_(ツ)_/¯

    Tuesday, October 14, 2014 11:52 PM