Issue
Is there a simple way to format a number to show at least n decimals ? If possible, using string templates and avoiding processing the string with code.
Expected results for 2 wanted decimals:
Value (type f) | Expected output |
---|---|
4 | 4.00 |
0.4 | 0.40 |
0.04 | 0.04 |
0.004 | 0.004 |
0.0004 | 0.0004 |
I've only found ways to specify fixed decimal precision through string templates.
Solution
I'm not quite sure this can be achieved with string templates alone, as your requirements are quite special. However if you specify the number of decimals in the string template, the trailing zeros can be cut off with a regex:
DATA numbers TYPE STANDARD TABLE OF f.
numbers = VALUE #(
( 4 / 1 )
( 4 / 10 )
( 4 / 100 )
( 4 / 1000 )
( 4 / 10000 )
).
LOOP AT numbers INTO DATA(number).
DATA(formatted) = replace(
val = |{ number DECIMALS = 4 }|
regex = '0{1,2}\z'
with = ''
).
WRITE formatted. NEW-LINE.
ENDLOOP.
Some comments:
- The \z operator matches the end of a string
DECIMALS = 4
was an arbitrary choice based on the examples, you might want to choose a larger number there, and then adapt the regex to match0{1, N - 2}
zeros.- In a very new system you might want to have a look at the new PCRE
Answered By - Jonas Wilms Answer Checked By - Cary Denson (PHPFixing Admin)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.