From: Alex DeCaria on
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/.

From: Josh Cheek on
[Note: parts of this message were removed to make it a legal post.]

On Mon, Apr 12, 2010 at 9:34 PM, Alex DeCaria <alex.decaria(a)millersville.edu
> 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.

From: Alex DeCaria on
Josh Cheek wrote:
> 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.

Here are some examples of what it should do:

Delete any characters other than digits, +, -, e, E, or .:
'-24.5fge4x'5 => '-24.5e45'

Delete any extra decimals:
'2.4.5' => '2.45'
'2..45' => '2.45'

Delete any decimals in an exponent:
'245e7.6' => '2.45e76'

Delete any extra or misplaced + or – signs:
'+45-68+e+45-' => '4568e+45'

Delete any extra or misplaced 'e' or 'E' characters (first occurance of
'e' or 'E' has precedence unless it doesn't make sense):
'4.67e6e-7' => '4.67e67'
'+e4.67e-7' => '+4.67e-7'

The motivation for this is for a GUI input textbox, so that if the user
enters a bad string it automatically corrects it to a valid
floating-point representation in string form before converting to a
floating-point for calculations. I toyed with just doing
str = str.to_f.to_s
and letting Ruby figure out the floating point respesentation, but I'd
like more control over how the string is converted to floating point
representation. For example, I want
'2..45e9' => '2.45e9', whereas '2..45e9'.to_f.to_s => '2.0'

--Alex


--
Posted via http://www.ruby-forum.com/.

From: Josh Cheek on
On Tue, Apr 13, 2010 at 6:20 AM, Alex DeCaria <alex.decaria(a)millersville.edu
> wrote:

> Delete any decimals in an exponent:
> '245e7.6' => '2.45e76'
>

Where did the dot in between 2 and 4 come from? Am I interpreting the String
or just cleaning it?


> Delete any extra or misplaced + or – signs:
> '+45-68+e+45-' => '4568e+45'
>
> Delete any extra or misplaced ‘e’ or ‘E’ characters (first occurance of
> '+e4.67e-7' => '+4.67e-7'
>
>
Why does the plus in front of 45 in the first one go away, but the plus in
front of the e in the second one stays?

-----

This is what I have so far, please check and correct any tests that should
be different

def clean_string( str , options = Hash.new )
str =~ /\A([-+]?)([^eE.]*\.?)([^eE]*)((?:[eE][+-]?)?)([^Z]*)\Z/
posneg , prepre , postpre , e , post = $1 , $2 , $3 , $4 , $5
posneg + prepre + postpre.gsub(/[^0-9]/,'') + e + post.gsub(/[^0-9]/,'')
end

require 'test/unit'
class TestCleanString < Test::Unit::TestCase
def test_delete_chars
assert_equal '-24.5e45' , clean_string('-24.5fge4x5')
end
def test_delete_extra_decimal
assert_equal '2.45' , clean_string('2.4.5')
assert_equal '2.45' , clean_string('2..45')
assert_equal '2.45' , clean_string('2...45')
end
def test_delete_extra_decimal_in_exponent
assert_equal '245e76' , clean_string('245e7.6') # you said this should
be '2.45e76' , but where did first dot come from?
end
def test_delete_extra_or_misplaced_pos_and_neg_signs
assert_equal '4568e+45' , clean_string('+45-68+e+45-')
end
def test_delete_extra_or_misplaced_e_or_E
assert_equal '4.67e67' , clean_string('4.67e6e-7')
assert_equal '+4.67e-7' , clean_string('+e4.67e-7')
end
end

From: Jean-Julien Fleck on
Hello,

2010/4/14 Josh Cheek <josh.cheek(a)gmail.com>:
> On Tue, Apr 13, 2010 at 6:20 AM, Alex DeCaria <alex.decaria(a)millersville.edu
>> wrote:
>
>> Delete any decimals in an exponent:
>> '245e7.6' => '2.45e76'
>>
>
> Where did the dot in between 2 and 4 come from? Am I interpreting the String
> or just cleaning it?

As said Josh, here you are interpreting the string rather than
cleaning it. 245e76 is a valid float, just not in the usual 2.45e78
form.

BTW, I would rather not do any cleaning under the hood: let the user
correct its input himself. For example, give the input to Float() and
if an error is raised (which Float does as opposed to to_f which never
raise an error), rescue it by giving feedback to the user (where you
could use your method to propose an alternative if you want) but do
not continue without letting the user know he has made a mistake and
giving him the ability to change his mind.

Cheers,

--
JJ Fleck
PCSI1 Lycée Kléber

 |  Next  |  Last
Pages: 1 2 3 4
Prev: [ANN]VTD-XML 2.8
Next: Rorth v0.3 released