A
Alex DeCaria
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
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