From: Josh Cheek Date: 2010-04-13T15:40:19+09:00 Subject: Re: Method to groom a string to floating point representation --000e0cd13a02ed4f290484188847 Content-Type: text/plain; charset=ISO-8859-1 On Mon, Apr 12, 2010 at 9:34 PM, Alex DeCaria wrote: > I have a program that asks for the user to enter a string that > represents a floating point number. Everytime a new character is typed > I want a method that checks to make sure the string makes sense as a > floating point number, and if not, deletes any bad characters. For > instance, if the user enters '4.5e+6.7' I want the method to delete the > extra decimal place and return '4.5e+67'. Or, if the user enters > something like '4.5+e7' it deletes the misplaced plus sign and returens > '4.7e7'. In short, I want the method to only allow correct > representations of floating point numbers, but I want it to remain as a > string. Anything other than a number or +, -, ., or e or E, should be > deleted. > > I wrote a method that works like I want (attached), but it is long and > cumbersome. I'm wondering if anyone has a shorter, better way to do > this. > > --Alex > > Attachments: > http://www.ruby-forum.com/attachment/4653/clean_string_lite.rb > > -- > Posted via http://www.ruby-forum.com/. > > It would probably be easier if you provided a set of tests we could check our function against, where we could be confident our function was correct once it passed all the tests. --000e0cd13a02ed4f290484188847--