[Jprogramming] String to Rational
Jimmy Gauvin
jimmy.gauvin at gmail.com
Tue Jan 5 16:14:28 UTC 2021
IF your strings always have two numbers after the decimal point you could
remove the point before converting to numeric.
NB.Remove decimal point
'.' -.~ each '123.45' ; '1456.12'
┌─────┬──────┐
│12345│145612│
└─────┴──────┘
NB. Convert to numeric
_ ". > '.' -.~ each '123.45' ; '456.12'
12345 45612
NB. type 4 is integer
3!:0 ] _ ". > '.' -.~ each '123.45' ; '456.12'
4
On Tue, Jan 5, 2021 at 10:43 AM Justin Paston-Cooper <
paston.cooper at gmail.com> wrote:
> Hello,
>
> I am reading in tab-separated files with readdsv from 'tables/dsv'.
> Some columns consist of exact decimal numbers like 6.66. These generally
> involve amounts of a decimal currency. These are read in by readdsv as
> boxed strings. makenum from 'tables/csv' converts these strings to
> floats. I would like these strings to be converted exactly to
> rationals. How can I go about this in the easiest and most efficient
> way? I could not find information on parsing data types from strings.
>
> Thanks in advance,
>
> Justin
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
>
More information about the Programming
mailing list