Switches to Set Indian Number Formatting for Amount Merge Fields in Word DOCX Document Java | Currency Decimal Format

Dear Team,

We have challenges in formatting numeric amounts in Word merge field. Currently, there is no such formatting available

Indian Format. 12,34,567.89
Standard Format| 1,234,567.89

The main difference is the comma position it should be after every two digits for all numbers and for last 3 numbers it should be 3 digits.

@saikrishna.p,

For standard number format, you can specify it like this:

{ MERGEFIELD mf \# "$#,##0.00;($#,##0.00)" }

C# Code to perform mail merge on such merge fields:

Document doc = new Document("C:\\Temp\\in.docx");
doc.MailMerge.Execute(new string[] { "mf" }, new object[] { 1234567.89 });
doc.Save("C:\\Temp\\20.7.docx");

Similarly, you need to find a number format that is suitable for Indian amount merge fields. Once you correctly configure amount number format of merge field in template document, MS Word and Aspose.Words should then be able to display correct result after mail merge operation.

We have tried this { MERGEFIELD mf # “$#,##0.00;($#,##0.00)” } with multiple options but there is no such format for Indian format. Have seen some postings that system should take Locale for amount formating. Expecting Indian format.

@saikrishna.p,

I think, you cannot achieve it using the merge field switches only, you should try the following:

  • Set the switch to just: { MERGEFIELD mf \# "#,#.00" }
  • And either set the Indian locale for the current thread or (more reliably) set number group sizes directly:

C# Code:

CultureInfo cultureInfo = new CultureInfo(CultureInfo.CurrentCulture.Name);
cultureInfo.NumberFormat.NumberGroupSizes = new int[] { 3, 2 };
Thread.CurrentThread.CurrentCulture = cultureInfo;
Document doc = new Document("C:\\Temp\\in.docx");
doc.MailMerge.Execute(new string[] { "mf" }, new object[] { 1234567.89 });
doc.Save("C:\\Temp\\20.7.docx");

Hi
Could not locate cultureInfo.NumberFormat.NumberGroupSizes in Java API.

Thanks

@saikrishna.p,

I am afraid, Aspose.Words for Java has not any analog for .NET’s CultureInfo.NumberFormat property and NumberFormat class. It only has a limited analogs for CultureInfo and DateTimeFormatInfo.

It is also not completely clear for us as to how to constantly display Indian Numbering Format on different Java and Android versions/platforms. Please look at the number of different answers in the following page:

So, the best way will be to pass a completely formatted string to the Mail Merge engine. Something like this:

doc.getMailMerge().execute(new String[] { "mf" }, new Object[] {format(1234567.89)});

Where format(double) will contain custom code for formatting a double to the Indian Numbering Formatted string. You can choose any method that works on your platform. For example:

  • NumberFormat.getCurrencyInstance(new Locale("en", "IN"))
  • new DecimalFormat("##,##,###.00")

or any other (the link above contains many options).