Help with regex

regex is not my cup of tea , i’ve been using them few times, only
matching, no substitution…

I need to check a string before saving it into a decimal (5,2) column
in DB and I actually don’t know how to start substitution :

the user is free to enter a value in any format, I must check if there
is any decimal (if any , then 2 digits are mandatory… it’s none or 2 !)

the user can enter any string like “99’999.99” or “99,999.99” or
“99999.99” or “99999”
should be substitued to "99999.99’

I would like also to accept the european way (comma before decimal part)
“99.999,99” or "99999,99’ should also be substitued to "99999.99’

so any input should be susbstitued to "99999.99’ takng in account
the decimal part if any

joss

Hello,

My first shot at this:

def to_nice_number(n)
frags = n.scan(/\d+/)
return n if frags.size == 1 #the trivial case “9999”
last_part = frags.pop
frags.join + “.” + last_part
end

Peter
http://www.rubyrailways.com

I would like also to accept the european way (comma before decimal part)
“99.999,99” or "99999,99’ should also be substitued to "99999.99’

“European” would be difficult without having context. How can you tell
if
999.999 is one short of a million or one thousanth short of a thousand?

Apart from that, do you want to verify that the seperator is correctly
placed:

66’000.95 is ok, but what about 650’00.95 or ,0001.95 ?

-tim

On 2006-10-11 15:37:03 +0200, “Tim B.” [email protected] said:

-tim

so any input should be susbstitued to "99999.99’ takng in account
the decimal part if any

joss

thanks for raising these cases,
all system amounts are purely informative … (normal case is below
9999.99)
and my concern is being flexible w validation

in this 3 cases the only thing the system cares is the decimal part :

66’000.95 will be normalized to 66000.95
650’00.95 will be normalized to 65000.95
,0001.95 will ne normalized to 0001.95 => 1.95
being accepted by the system, the use will see on display that they are
not as they should be
and the user will correct them later on…

On 2006-10-11 15:18:05 +0200, Peter S. [email protected]
said:

Peter
http://www.rubyrailways.com

Thanks Peter… actually I don’t have yet the ‘ruby way’… using scan
, pop !! that’s it
I tested in all cases and it works …

I also register your RSS

On 11.10.2006 16:59, Josselin wrote:

On 2006-10-11 15:37:03 +0200, “Tim B.” [email protected] said:

I would like also to accept the european way (comma before decimal part)
“99.999,99” or "99999,99’ should also be substitued to "99999.99’

“European” would be difficult without having context. How can you tell if
999.999 is one short of a million or one thousanth short of a thousand?

Apart from that, do you want to verify that the seperator is correctly
placed:

all system amounts are purely informative … (normal case is below
9999.99)
and my concern is being flexible w validation

In that case it is even more important that you get it crystal clear (at
least for you) what values you will be accepting and how you will
interpret them! And then write a set of test cases that cover every
possible input you can imagine plus those that you cannot imagine.

in this 3 cases the only thing the system cares is the decimal part :

66’000.95 will be normalized to 66000.95
650’00.95 will be normalized to 65000.95
,0001.95 will ne normalized to 0001.95 => 1.95
being accepted by the system, the use will see on display that they are
not as they should be
and the user will correct them later on…

Frankly, if I were you, I would code to reject the last one. I may be
missing something here but I do not know any notation / locale in which
this would be legal.

It is difficult to get i18n right, as you might start to imagine. :slight_smile:
If you can, try to add some contextual information (e.g. user’s locale)
and - even better - utilize some library code for this.

My 0.02EUR…

Kind regards

robert

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs