What is the correct way to round numbers to 2 deci...
# advancedpdf
l
What is the correct way to round numbers to 2 decimals in advanced pdf? I tried this from a NS article but I am getting an unexpected error: ${‌item.rate?string(“0.##“)}
c
Are those proper quotation marks? "
l
Yes, I also tried single quotes
I also tried these: “”
e
I do think its the quotation marks that are the issue. Try copy/pasting this: ${‌item.rate?string("0.##")}
c
I did leave a spare one after my comment. My bad. Should have left 2 of them for him to use....
😆 1
e
lol, I think you nailed the issue. I think it's a mac thing when you try to copy and paste " from other places. It sometimes turns them into those slanted one
d
TIL they're called "curly quotation marks" (AKA “Smart Quotes” or ‘typographer's quotes’)
l
I think I tried all of them but still getting the same error. Does it make a difference if my variable is a calculated one using #assign? ${totalunapplied?string(“0.##“)}
d
shouldn't do. just double check what `totalunapplied`'s value is. Also check it with
${totalunapplied?is_number}
Is the error you're getting just "unexpected error" or is there more? Also, maybe you could share more of the template, like the assign directive
l
It works fine without the rounding code. Here’s the error:
d
did you try
?is_number
l
Here’s the code: <table class=“itemtable” style=“width: 100%; margin-top: 10px;“><!-- start items --> <#assign totalunapplied = 0> <thead> <tr> <th colspan=“3”>${record.lines.datecol@label}</th> <th colspan=“12">${record.lines.description@label}</th> <th align=“right” colspan=“3">${record.lines.charge@label}</th> <th align=“right” colspan=“3">${record.lines.payment@label}</th> <th align=“right” colspan=“3">Unapplied</th> <th align=“right” colspan=“3">${record.lines.balance@label}</th> </tr> </thead> <#list record.lines as line><tr> <td colspan=“3”>${line.datecol}</td> <td colspan=“12">${line.description}</td> <td align=“right” colspan=“3">${line.charge}</td> <td align=“right” colspan=“3">${line.payment}</td> <td align=“right” colspan=“3">${line.amountremaining}</td><#assign totalunapplied = (line.amountremaining + totalunapplied)> <td align=“right” colspan=“3">${line.balance}</td> </tr> </#list> <tr> <td colspan=“3”></td> <td colspan=“12"></td> <td align=“right” colspan=“3"></td> <td align=“right” colspan=“3"></td> <td align=“right” colspan=“3">${totalunapplied?string(“0.##“)}</td> <td align=“right” colspan=“3"></td> </tr></table>
@David B I’ll try that. Thanks.
@David B, oh that’s boolean. It return “No”. Why is it not treated like a number though?
d
yeah, seems like it might be a string. What value do you see if you you just print
${totalunapplied}
? Confirm it's a string with
?is_string
, and then you may need to use
?number?string('0.##')
l
100.000 (3 decimals) for example
I’ll try that too. Thanks a lot.
Same unexpected error. Uggh didn’t expect this to be this complicated. haha
d
okay, only got a few ideas left on me • change the increment assign to
<#assign totalunapplied += line.amountremaining?number>
(amount remaining might be coming out as a string) • change the
?string('0.##')
to
?string['0.##']
. The latter is the newer way, but the former should still be working.
not sure if you'd also need to do some null-checking on amountremaining (see Handling missing values)
p
${totalunapplied?string["0.##"]} is the correct way of applying that rounding
sorry about the late reply, vacay 😛
Btw, you can do something like this as well:
<#if item.rate?has_content>${item.rate}<#else><#assign zero=0.00>${zero?string.currency}</#if>