Not so serious question... I am formatting numbers...
# advancedpdf
m
Not so serious question... I am formatting numbers this way
amount?string('#,##0.00;; decimalSeparator="." groupingSeparator=_')?replace("_", "'")
because I fail to set
'
after
groupingSeparator=
it always gives errors, any ideas how it should be done, without using
?replace
?
e
does `amount?string(
#,##0.00;; decimalSeparator="." groupingSeparator='
)` work? I think you are having an issue with setting
'
because you are also using the same symbol as your open/close string delimeter
m
Syntax error. I think tried it before. This also gives syntax error ``#,##0.##;; decimalSeparator="." groupingSeparator="'"`` it is just NS being weird, or freemarker being weird, I don't know. I went with another solution... thanks for your help
d
may have found something in the docs should be able to use:
amount?string('#,##0.00;; decimalSeparator="." groupingSeparator=''')
will test myself later
(you use two apostrophes/quotation marks)
m
@David B Thanks, I think I missed that. But it still didn't work. Looks like it reads this part as an argument
'#,##0.00;; decimalSeparator="." groupingSeparator='
and
''
as another argument.
d
bother, sorry about that =/
🙏 1
s
For currencies, sometimes the source value is a string, so that confuses things occassionally. In all of my advpdf work, I add a custom function:
<#function num value>
<#assign rawvalue = value?replace("$","")?replace(",","") />
<#if rawvalue?length gte 1>
<#return rawvalue?number />
<#else>
<#return 0 />
</#if>
</#function>
Then when I want to render a string, at least I know it's definitely a number I'm working with. Then you can
${num({field})?string.("$#,##0.00")
or
${num({field})?string.("€#.##0,00")
or whatever else you need. In the function you could use a regex to remove all non-numeric/decimal characters as well.
m
@Stefan Reeder Here's the thing... number formats depend on the document locale, so
?replace(",", "")
here is not safe, because with some formats the comma could be a decimal separator, not a group separator.
d
there's possibly a regex pattern that would work, let me check
That was a fun but pointless exercise. But for anyone regex inclined: https://regex101.com/r/bH8K2X/4 Formats being tested came from the Oracle Solaris docs
m
Thanks for taking the time to write this. Why do you think it is pointless?
I am not sure how to use it actually...
Maybe that's why you are saying it is pointless. You only need the parts before and after the decimal separator. But this result won't help.
But wow, man. Regular expressions feel like a superpower.
s
It could be shortened i think. Numbers only, then specify the decimal character
d
yeah, can definitely be shorter if you make some assumptions about the possible formats. For example, if there are no thousands separators, no currency symbol, but you don't know the decimal character, you could use this simple pattern:
^(\d+)([.,]\d+)?$
which would work on numbers like:
4294967295.00
4294967295,00
4294967295
s
I'm a bit rusty on regex. Can you nominate to keep only numbers, and the "last" "." or ","?
d
for keeping only numbers, you just run the 'first' match (i.e. left of the decimal char) through a replace (
leftMatch.replace(/[ ,.]/g,'')
) Identifying the last
.
,
is tricky, as sometimes it's the decimal char, like in
1,234.000
,
1.234,000
, or
1 234,000
, but other times it's just another thousands separator, like in
1,234,000
or
1.234.000
Then you get awkward strings like
1.000
or
1,000
. Could be 1 or 1000 depending on what locale is being used...
maybe regex isn't the right tool... https://xkcd.com/1171/