How to round numbers in Aspose

Hey

I’m trying to create a report using Aspose using some custom attributes like TotalSum (i define them myself)

There is a construction I use:

<<var [grammar = new GrammarScriptlet()]>>
<<[base.TotalSum]:“#,###.00”>> <<[grammar.getMoney(new BigDecimal(base.TotalSum))]>>

the expected result is smth like that
1 000.20 (one thousand dollars twenty cents)

but sometime it gives the result like this:
1 000.20 (one thousand dollars nineteen cents)

I investigated this and the reason is that <<[base.TotalSum]:“#,###.00”>> rounds the number and <<[grammar.getMoney(new BigDecimal(base.TotalSum))]>> doesn’t

I need these two atributes to match perfectly, if it’s 0.20 cents then it should be twenty cents, not nineteen.

How can I round a number in Aspose? Before using a getMoney function.

I searched here Appendix A. Enumeration Extension Methods in C#|Aspose.Words for .NET and got nothing.

Please help. Thank you.

@Random_System_Analyst,
If you are using BigDecimal class in your template document, you can round your value in the way, shown bellow:

(new BigDecimal(1000.1999)).setScale(2, RoundingMode.HALF_UP);

If you still facing the problem, please share sample code you used to get an output document. We will then provide you code example and/or updated template document needed to get desired result according to your requirement.

Thank you
Problem is still there.
This is an input
image.png (20.2 KB)
And this is an output
image.png (13.2 KB)

And when I try to use your version:
image.png (9.5 KB)

and the output is
image.png (2.2 KB)
I guess something’s wrong with round brackets.
Is that enough info?

@Random_System_Analyst Could you please attach your sample template and code you use? This will allow us to reproduce the problem and significantly simplify investigation of the issue on our side.

it is actually confidential data but I can anonymize it

the template is here (my file is RTF but its prohibited here so there is a screenshot and the text itself:
image.png (7.7 KB)

<<[debt.TABLE.last().PENALTYcustomAttr123]:”#,###.00”>> <<[debt.last().currencyName]>> (<<[grammar.getMoney(new BigDecimal(debt. TABLE.last().PENALTYcustomAttr123))]>>)

<<[debt.TABLE.last().PENALTYcustomAttr123]:”#,###.00”>> <<[debt.last().currencyName]>> (<<[grammar.getMoney(new BigDecimal(debt.TABLE.last().PENALTYcustomAttr123)).setScale(2, RoundingMode.HALF_UP)]>>)

and the custom entity code is this:
[{
“name”: “TABLE”,
“select”:
{
“PENALTYcustomAttr123”: “t.custom_attr123”,
},
“from”: “DBTABLE t”,
“parent”:
{
“entityTypeId”: 19,
“parentKey”: “t.r_entity_id”
}
}]

the result is in my previous comment
first part (my) gives answer without rounding, like
100.20$ (one hundred dollars nineteen cents)
and the second one (from this thread) gives answewr with empty round brackets
100.20$ ()

@Random_System_Analyst I have checked rounding approach suggested by Sergey and it works as expected on my side:
Template:

<<[(new BigDecimal(TotalSum)).setScale(2, RoundingMode.HALF_UP)]>>

And the code:

ReportingEngine engine = new ReportingEngine();
engine.getKnownTypes().add(BigDecimal.class);
engine.getKnownTypes().add(RoundingMode.class);
engine.buildReport(doc, 1000.1999, "TotalSum");

Here are input and output documents in.docx (12.1 KB)
out.docx (9.5 KB)

I have rechecked your template syntax and there is a mistake with parentheses. Please try using the following syntax:

<<[debt.TABLE.last().PENALTYcustomAttr123]:”#,###.00”>> <<[debt.last().currencyName]>> (<<[grammar.getMoney((new BigDecimal(debt.TABLE.last().PENALTYcustomAttr123)).setScale(2, RoundingMode.HALF_UP))]>>)

If this does not help, please attach your GrammarScriptlet class used for generating numbers text representation?