Convert Strings To Numbers

Welcome Forums General PowerShell Q&A Convert Strings To Numbers

Viewing 9 reply threads
  • Author
    Posts
    • #222444
      Participant
      Topics: 1
      Replies: 5
      Points: 30
      Rank: Member

      I have some script that outputs string values where they should be numeric values (with 2 decimal places) and I just need the script to output them with numeric values instead of strings with quotation marks around them.  Please help.

      https://mega.nz/file/90t3DLLT#R9GZBR3lpOmLQaHqNCURKDl10B1kaVLj2Ra2SdvLcko

      • This topic was modified 1 month ago by kvprasoon. Reason: move to right forum
    • #223134
      Senior Moderator
      Topics: 8
      Replies: 1215
      Points: 4,335
      Helping Hand
      Rank: Community Hero

      Please use gist.github.com to post the code instead of sharing the script via other cloud storages. Not every one would have access to all the cloud storages.

      Without looking at your code, in general you can type cast your output to the desired type you want.

      $r = '1' # this is a string
      [int]$r # this becomes integer
      $r -as [int] # this also becomes integer
      $r -as [float]
      
    • #223428
      Participant
      Topics: 1
      Replies: 5
      Points: 30
      Rank: Member

      Membership is not required to download my file from the link that I provided.  However, maybe it’s better to post the code.  This code outputs the required numeric fields as text in quotations and I need to have all of them output as numeric values.  Your help is appreciated.  Also, I don’t know how to post code in this forum.

      https://pastebin.com/reL1djZm

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

      #
      # Get-SOHLCV.ps1
      #
      #
      #
      [CmdletBinding()]
      param (
      [string[]]
      $File,
      [switch]
      $ExportAsObject
      )
      function Convert-CurrencyStringToDecimal ([string]$input)
      {
      ((($input -replace '\$') -replace '[)]') -replace '\(', '-') -replace '[^-0-9.]'
      }
      $global:sohlcvData = New-Object System.Collections.Generic.List[PSCustomObject]
      foreach ($f in $File)
      {
      if (-not (Test-Path $f))
      {
      throw "Cannot open file '$f'."
      }
      # read csv file
      $content = Get-Content -Path $f
      # find the lines that contain price information
      $csvdata = $content | ? {$_ -match ";.*;"} | ConvertFrom-Csv -Delimiter ';'
      # filter just the lines with (OHLC on them and make into CSV structure
      $data = $csvData | ? {$_ -match "\(SOHLCV"}
      foreach ($item in $data)
      {
      # capture the OHLCV data
      $null = $item.Strategy -match "\(SOHLCV\|(.*)\)"
      $v = $Matches[1] -split '\|'
      $symbol = $v[0]
      $open = $v[1] | Convert-CurrencyStringToDecimal
      $high = $v[2] | Convert-CurrencyStringToDecimal
      $low = $v[3] | Convert-CurrencyStringToDecimal
      $close = $v[4] | Convert-CurrencyStringToDecimal
      $volume = $v[5] | Convert-CurrencyStringToDecimal
      # add to our $sohlcvData array
      $null = $sohlcvData.Add(
      [PSCustomObject]@{
      'Symbol' = $symbol
      'DateTime' = ([datetime]::Parse($item.'Date/Time'))
      'Open' = [decimal]$open
      'High' = [decimal]$high
      'Low' = [decimal]$low
      'Close' = [decimal]$close
      'Volume' = [decimal]$volume
      }
      )
      }
      }
      if ($ExportAsObject)
      {
      # helpful message to show caller our output variable
      Write-Output "Out Data $($sohlcvData.Count) items (exported as `$sohlcvData)"
      }
      else
      {
      # don't show any output, and just return the data to the pipeline
      return $sohlcvData
      }
      

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

      • This reply was modified 1 month ago by Picard08.
      • This reply was modified 1 month ago by Picard08.
    • #223638
      Senior Moderator
      Topics: 8
      Replies: 1215
      Points: 4,335
      Helping Hand
      Rank: Community Hero

      well, you would have seen a pinned post in the forums titled “Read Me Before Posting! You’ll be Glad You Did!”

      https://powershell.org/forums/topic/read-me-before-posting-youll-be-glad-you-did/
      Is you problem in $sohlcvData variable’s value ?

    • #223908
      Participant
      Topics: 1
      Replies: 5
      Points: 30
      Rank: Member

      kvprasoon,

      In the code there are string text for “Symbol”, “Date/Time”,”Open”, “High”,”Low”,”Close”,”Volume”.  The output that I need is that Symbol be the only text field, the Date/Time field be Date/Time format and all the others be numeric.  How can this be changed?

      This what the output currently looks like:

      “Symbol”,”DateTime”,”Open”,”High”,”Low”,”Close”,”Volume”
      “SPYV”,”6/20/2016 6:40:00 AM”,”25.108″,”25.108″,”25.108″,”25.108″,”412″
      “SPYV”,”6/20/2016 6:42:00 AM”,”25.0725″,”25.0725″,”25.0725″,”25.0725″,”1236″
      “SPYV”,”6/20/2016 7:30:00 AM”,”25.0867″,”25.0867″,”25.0867″,”25.0867″,”1572″
      “SPYV”,”6/20/2016 7:32:00 AM”,”25.0926″,”25.0926″,”25.0926″,”25.0926″,”400″

    • #223950
      Participant
      Topics: 3
      Replies: 340
      Points: 1,120
      Helping Hand
      Rank: Community Hero

      Not sure what’s going on here. I know that with your pscustomobject and the values you gave, they are typed properly at this point.

      
      $obj = [PSCustomObject]@{
      ‘Symbol’ = "SPYV"
      ‘DateTime’ = ([datetime]"6/20/2016 7:32:00 AM")
      ‘Open’ = [decimal]"25.0926"
      ‘High’ = [decimal]"25.0926"
      ‘Low’ = [decimal]"25.0926"
      ‘Close’ = [decimal]"25.0926"
      ‘Volume’ = [decimal]"1400"
      }
      
      $obj |gm
      
      
      
      Close NoteProperty decimal Close=25.0926
      DateTime NoteProperty datetime DateTime=6/20/2016 7:32:00 AM
      High NoteProperty decimal High=25.0926
      Low NoteProperty decimal Low=25.0926
      Open NoteProperty decimal Open=25.0926
      Symbol NoteProperty string Symbol=SPYV
      Volume NoteProperty decimal Volume=1400
      
      
    • #224106
      Participant
      Topics: 1
      Replies: 5
      Points: 30
      Rank: Member

      I’m not sure if this will help, but this is the raw data that gets processed.  This is raw data, not code.
      ======================================

      Strategy report
      Symbol: SPYV
      Work Time: 6/20/16 6:40 AM - 7/1/16 12:55 PM
      
      Id;Strategy;Side;Amount;Price;Date/Time;Trade P/L;P/L;Position;
      1;kg_EveryTickSOHLCV(SellClose);Sell to Close;-1.0;$25.13;6/20/16 6:40 AM;$0.00;$0.02;-1.0;
      2;kg_EveryTickSOHLCV(SOHLCV|SPYV|25.108|25.108|25.108|25.108|412);Buy to Open;1.0;$25.13;6/20/16 6:40 AM;;$0.00;0.0;
      3;kg_EveryTickSOHLCV(SellClose);Sell to Close;-1.0;$25.11;6/20/16 6:42 AM;$0.00;$0.04;-1.0;
      4;kg_EveryTickSOHLCV(SOHLCV|SPYV|25.0725|25.0725|25.0725|25.0725|1,236);Buy to Open;1.0;$25.11;6/20/16 6:42 AM;;$0.00;0.0;
      5;kg_EveryTickSOHLCV(SellClose);Sell to Close;-1.0;$25.07;6/20/16 7:30 AM;$0.00;($0.01);-1.0;
      6;kg_EveryTickSOHLCV(SOHLCV|SPYV|25.0867|25.0867|25.0867|25.0867|1,572);Buy to Open;1.0;$25.07;6/20/16 7:30 AM;;$0.00;0.0;
      7;kg_EveryTickSOHLCV(SellClose);Sell to Close;-1.0;$25.09;6/20/16 7:32 AM;$0.00;($0.01);-1.0;
      8;kg_EveryTickSOHLCV(SOHLCV|SPYV|25.0926|25.0926|25.0926|25.0926|400);Buy to Open;1.0;$25.09;6/20/16 7:32 AM;;$0.00;0.0;
      9;kg_EveryTickSOHLCV(SellClose);Sell to Close;-1.0;$25.09;6/20/16 7:34 AM;$0.00;$0.05;-1.0;
      10;kg_EveryTickSOHLCV(SOHLCV|SPYV|25.0425|25.0467|25.04|25.0467|8,000);Buy to Open;1.0;$25.09;6/20/16 7:34 AM;;$0.00;0.0;
      11;kg_EveryTickSOHLCV(SellClose);Sell to Close;-1.0;$25.05;6/20/16 8:17 AM;$0.01;($0.02);-1.0;
      12;kg_EveryTickSOHLCV(SOHLCV|SPYV|25.067|25.067|25.067|25.067|448);Buy to Open;1.0;$25.04;6/20/16 8:17 AM;;$0.01;0.0;
      13;kg_EveryTickSOHLCV(SellClose);Sell to Close;-1.0;$25.07;6/20/16 8:28 AM;$0.00;$0.00;-1.0;
      14;kg_EveryTickSOHLCV(SOHLCV|SPYV|25.0725|25.0725|25.0725|25.0725|464);Buy to Open;1.0;$25.07;6/20/16 8:28 AM;;$0.01;0.0;
      15;kg_EveryTickSOHLCV(SellClose);Sell to Close;-1.0;$25.07;6/20/16 9:14 AM;$0.00;$0.05;-1.0;
      16;kg_EveryTickSOHLCV(SOHLCV|SPYV|25.0285|25.0285|25.0285|25.0285|1,856);Buy to Open;1.0;$25.07;6/20/16 9:14 AM;;$0.01;0.0;
      17;kg_EveryTickSOHLCV(SellClose);Sell to Close;-1.0;$25.03;6/20/16 10:15 AM;$0.00;$0.02;-1.0;
      18;kg_EveryTickSOHLCV(SOHLCV|SPYV|25.0177|25.0177|25.0177|25.0177|412);Buy to Open;1.0;$25.03;6/20/16 10:15 AM;;$0.01;0.0;
      19;kg_EveryTickSOHLCV(SellClose);Sell to Close;-1.0;$25.02;6/
      Max trade P/L: $0.57;
      
      Total P/L: $1.24;
      
      Total order(s): 198;
      • This reply was modified 1 month ago by Picard08.
      • This reply was modified 4 weeks, 1 day ago by Picard08.
    • #224109
      Participant
      Topics: 4
      Replies: 2247
      Points: 5,484
      Helping Hand
      Rank: Community MVP

      Please, it does not help when you’re not formatting it properly.

      When you post code or error messages or sample data or console output format it as code, please.
      In the “Text” view you can use the code tags “PRE“, in the “Visual” view you can use the format template “Preformatted“. You can go back edit your post and fix the formatting – you don’t have to create a new one.
      Thanks in advance

    • #224169
      Participant
      Topics: 1
      Replies: 5
      Points: 30
      Rank: Member

      Thanks Olaf,
      I didn’t know how to do that.

      I still need help with this, if anyone is available to solve this issue.
      Anyway, if I haven’t provided enough explanation of this project then maybe these links would help. I appreciate the help that you guys are giving me.

      All Project Files
      https://mega.nz/file/4tckTQSB#voXExrndNk_PvDMC8880XjioVl946FVAu_i_uRZ4HZ0

      Explanation of Using the Code

      • This reply was modified 1 month ago by Picard08.
    • #225396
      Participant
      Topics: 1
      Replies: 5
      Points: 30
      Rank: Member

      The exported data needs to be in this format.

      IEOD Data Format

Viewing 9 reply threads
  • You must be logged in to reply to this topic.