I suspect you are messing around with meaningless precision if these numbers are coming from a spreadsheet since they are already in floating point format there but this may do what you need:
stringToRational=: 3 : 0 if. 1=#tmp=. <;._1 '.',y do. tmp=. tmp,<,'0' end. ".(;tmp),'r1','0'$~#;1{tmp ) stringToRational &> '123.00';'123';'1';'1010101010.10101';'0.1';'2.';'123456789101112.1314151617' 123 123 1 101010101010101r100000 1r10 2 1234567891011121314151617r10000000000 NB. Visually verifying the ridiculous ones: 28j10":"0 ] 101010101010101r100000 1234567891011121314151617r10000000000 1010101010.1010100000 123456789101112.1314151617 On Wed, Jan 6, 2021 at 11:04 AM Henry Rich <henryhr...@gmail.com> wrote: > For domain errors, Look at the Dissect tool. > > Henry Rich > > On 1/6/2021 3:40 AM, Justin Paston-Cooper wrote: > > I should add how it works: > > > > On the right, 10^(The position of '.' in the number minus 1) > > On the left, remove '.' and parse as number > > In the middle, apply x: to both sides and divide. > > > > Also, how can I go about actually debugging domain errors? Both sides > > seem to be zero-dimensional. > > > > On Wed, 6 Jan 2021 at 10:20, Justin Paston-Cooper > > <paston.coo...@gmail.com> wrote: > >> Thanks for the suggestion. I've combined Don's idea of using x: and > >> your idea of removing the point and parsing that to handle all numbers > >> of digits after the decimal point: > >> > >> stringToRational =: ((_&".)@(-.&'.') %&:x: 10&^@(<:@# - (i.&'.')) > >> > >> stringToRational '666.66' should give 333r50. > >> > >> Sadly this is giving a domain error, even though both sides of the > >> fork work alone. Could anyone suggest how to fix this? > >> > >> On Tue, 5 Jan 2021 at 19:14, Jimmy Gauvin <jimmy.gau...@gmail.com> > wrote: > >>> 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.coo...@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 > >>>> > >>> ---------------------------------------------------------------------- > >>> For information about J forums see http://www.jsoftware.com/forums.htm > > ---------------------------------------------------------------------- > > For information about J forums see http://www.jsoftware.com/forums.htm > > > -- > This email has been checked for viruses by AVG. > https://www.avg.com > > ---------------------------------------------------------------------- > For information about J forums see http://www.jsoftware.com/forums.htm > -- Devon McCormick, CFA Quantitative Consultant ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm