Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

"Input string was not in a correct format" error

$
0
0

Hi Experts,

the following code keep getting this error message. What's wrong with my code? Please help me. Thanks a lot!

===============================================================

Cannot convert value "$4.42" to type "System.Decimal". Error: "Input string was not in a correct format."
At C:\Assignment\test_run_conversion02.ps1:27 char:17
+                 $FinalPrice = ([decimal]$BookListPrice + [decimal]$TaxPayable)
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvalidCastFromStringToDecimal

=================================================================

My code:

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

New-variable $TaxPayable -description decimal
New-Variable $HST -Value 13% -Option Constant
New-Variable $PurchasePrice -Description decimal

#[decimal]$HST = 0.13

clear-host
$options = 0

Do 
  {
  $options = Read-Host "please enter a number from 1-7"
  $BookListPrice = Read-Host "Please enter the book's list price"
 
          while ($BookListPrice -NotMatch "[0-9]+(\.[0-9]+)?")
                {
                    $BookListPrice = Read-Host "Please enter the correct format in decimal"
                                       
                    continue
                }
                               
                $TaxPayable = "{0:C2}" -f ([decimal]$BookListPrice * $HST)

                $taxtype = $TaxPayable.GetType()
                Write-Host $Taxtype

                $FinalPrice = ([decimal]$BookListPrice + [decimal]$TaxPayable)

                $PurchasePrice = "{0:C2}" -f $FinalPrice

                Write-Host ""
                               
                Write-Host "The list price of your book is $BookListPrice"
                Write-Host "Tax payable is $TaxPayable"
                Write-Host "=============================================="
                Write-Host "Purchase Price is $PurchasePrice"
               
   }
   while (($options -ge 1) -and ($options -le 7))
 
  write-host "thank you"
  exit 
 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


Viewing all articles
Browse latest Browse all 6937

Trending Articles