‎2008 May 09 3:09 AM
All,
I need to remove trailing zeros in numbers. Given the value is held in a decimal variable called "duration", how do I achieve the following when outputting to String:
"Duration" value Required Output
1.550 ---------> 1.55
1.5000 ---------> 1.5
2.0000 ---------> 2
0.0100 ---------> 0.01
I am doing this for PHP scripting
a®
‎2008 May 09 4:00 AM
Not 100% clear from the question if you wanted to do this with RegEx in PHP or just within ABAP... for the latter, there are probably several ways but this is the first that leapt to mind:
report zlocal_jc_sdn_regex.
data:
g_number type p decimals 4.
start-of-selection.
*1.550 ---------> 1.55
g_number = '1.550'.
perform fix_it.
*1.5000 ---------> 1.5
g_number = '1.5000'.
perform fix_it.
*2.0000 ---------> 2
g_number = '2.0000'.
perform fix_it.
*0.0100 ---------> 0.01
g_number = '0.01'.
perform fix_it.
*&---------------------------------------------------------------------*
*& Form fix_it
*&---------------------------------------------------------------------*
form fix_it.
data:
l_char(20) type c.
write: g_number to l_char right-justified.
shift l_char right deleting trailing '0 0 0 0 0 0 0 0 0 0 0 0 0 0 0'.
shift l_char right deleting trailing '.'. "for NNN.0000
shift l_char left deleting leading space.
write: / g_number, '--------->', l_char.
endform. "fix_it
which gives:
1.5500 > 1.55
1.5000 > 1.5
2.0000 > 2
0.0100 > 0.01
Jonathan
‎2008 May 09 4:00 AM
Not 100% clear from the question if you wanted to do this with RegEx in PHP or just within ABAP... for the latter, there are probably several ways but this is the first that leapt to mind:
report zlocal_jc_sdn_regex.
data:
g_number type p decimals 4.
start-of-selection.
*1.550 ---------> 1.55
g_number = '1.550'.
perform fix_it.
*1.5000 ---------> 1.5
g_number = '1.5000'.
perform fix_it.
*2.0000 ---------> 2
g_number = '2.0000'.
perform fix_it.
*0.0100 ---------> 0.01
g_number = '0.01'.
perform fix_it.
*&---------------------------------------------------------------------*
*& Form fix_it
*&---------------------------------------------------------------------*
form fix_it.
data:
l_char(20) type c.
write: g_number to l_char right-justified.
shift l_char right deleting trailing '0 0 0 0 0 0 0 0 0 0 0 0 0 0 0'.
shift l_char right deleting trailing '.'. "for NNN.0000
shift l_char left deleting leading space.
write: / g_number, '--------->', l_char.
endform. "fix_it
which gives:
1.5500 > 1.55
1.5000 > 1.5
2.0000 > 2
0.0100 > 0.01
Jonathan
‎2008 May 09 4:09 AM
‎2008 May 09 7:47 AM
I'm on an ECC5 system at the moment so those classes don't exist... but that didn't stop me trying!... I used http://www.solmetra.com/scripts/regex/index.php to test various patterns out and this appeared to be close:
"(\.0000\t?)|([0\t]+?)$"
with the various options generated calls like
preg_replace('"(\.0000\t?)|([0\t]+?)$"', '', '1.5500');
returned results like
string(4) "1.55"
... but this is the first time I've ever tried a RegEx so may have missed the point (give me ABAP any day!)...
Jonathan
‎2008 May 09 3:08 PM
Thanks Jonathan,
The link you provided help me lot http://www.solmetra.com/scripts/regex/index.php
Here is my RegEx
move '(?:(.d*[1-9])|.)0*$' to p_regex.
condense p_regex.
create object regex
exporting
pattern = p_regex
ignore_case = ''.
* For REGEX match
matcher = cl_abap_matcher=>create(
pattern = p_regex
ignore_case = ' '
table = i_files ).
lt_result = matcher->replace_all( ).
Regarding sub matches i am not worrying about that currently.
a®