In SilkTest Classic IDE the following code will convert a string “currency” value to a number value (thus allowing you to perform numeric operations) and then back to a string value for ease of formatting. This is done by implementing the following two functions:

[-] number CurrencyToNumber(string sString)
   [ ]
   [ ] string sRemoveCurrency = stuff (sString, 1, 1, "")
   [ ]
   [-] while (StrPos(",", sRemoveCurrency) > 0)
      [ ] sRemoveCurrency = stuff(sRemoveCurrency, StrPos (",", sRemoveCurrency),1, "")
   [ ]
   [ ] return (val(ltrim(sRemoveCurrency)))

[-] string NumberToCurrency(number nNumber)
   [ ] string sCurrency = "{nNumber}"
   [ ]
   [ ] INT iPoint = StrPos(".",sCurrency) +2
   [ ] sCurrency = SubStr(sCurrency,1, iPoint)
   [ ] sCurrency = "${sCurrency}"
   [ ] return sCurrency

[-] testcase subtract() appstate none
  [ ] NUMBER nTemp1, nTemp2, nReturnedValue
  [ ]
  [ ] nTemp1 = CurrencyToNumber("$2,456.89")
  [ ] nTemp2 = CurrencyToNumber("$1,345.34")
  [ ] nReturnedValue = nTemp1 - nTemp2
  [ ]
  [ ] print(NumberToCurrency(nReturnedValue))

The above code when executed will return:

[-] Testcase subtract - Passed
[ ] $1111.55


The following VB.NET function will convert a string “currency” value to an equivalent Double numeric value:

Public Module Main

Public Function Currency2Number(strCurrency As String) As Double
   Dim Currency As Double
   Dim strTemp As String
   Dim I As Integer

   I = strCurrency.Length
   strTemp = strCurrency.Substring(1,I-1)

   If Double.TryParse(strTemp, Currency) Then
      Console.Write("Numeric value of Currency = ")
      Console.WriteLine(Currency)
   Else
      Console.WriteLine("Unable to parse '{0}'.", Currency)
  End If
  Return Currency
End Function

Public Sub Main()
   Currency2Number("$4599.78")
End Sub

End Module

The above code when executed will return:

.NET Script 'StringToNumber' Started
Numeric value of Currency = 4599.78
.NET Script 'StringToNumber' Completed

Incident #2587836