Search code examples
j

Can I change datatype in j


I loaded my data in a table in j. Since they are all prices and I need to calculate returns, I realize that the datatypes of all cells are literal. How can I change it to floating type? Thanks!


Solution

  • As long as the literals are all numerals you can use dyadic ".(Numbers)

    http://www.jsoftware.com/help/dictionary/d601.htm

    The left argument is a numeral that will replace any illegal characters.

       3 10 $ '23 223 1  3 5 334.05 1 4 3.6e3' NB. String
    23 223 1  
    3 5 334.05
     1 4 3.6e3
       0 ". 3 10 $ '23 223 1  3 5 334.05 1 4 3.6e3' NB. conversion to numeric
    23 223      1
     3   5 334.05
     1   4   3600
       2 * 0 ". 3 10 $ '23 223 1  3 5 334.05 1 4 3.6e3' NB. Multiplying wouldn't work if it was a string!
    46 446     2
     6  10 668.1
     2   8  7200
    

    There may be some work to be done to be sure that the literal table is all numeric symbols ( ". won't convert '$' or alphabet characters.

       0 ". '$23.01' NB. returns 0 since '$'is not numeric
    0