Aspose.Word Number Format issue with # showing spaces

In text form fields, we use Number Format = “#,##0;(#,##0);0”.

When using Word Automation for merging, when displaying a negative number (e.g. -5) is would show as “(5)”.

However since switching to Aspose Word, the hashes (#) are interpreted as being spaces, when a value is not present, so this shows as “( 5)”.

Unfortunately our client is not happy with this, so we need a way to get Aspose.Words to behave the same as Microsoft Word.

Any idea how to achieve this?

@andrew.cousins Please attach the source code and input document here, we will check the issue and provide you with additional information.

Thanks @Vadim.Saltykov - here is a code sample below. We use form fields and replace variables in the calculation field with their actual values, and then set the TextInputDefault to resolve the calculation and apply formatting.

// get all form fields
var formFields = _doc.Range.FormFields.Where(x => x.TextInputType == TextFormFieldType.Calculated).ToList();

// Code to replace variables with actual values from a data table goes here and we set the "formula" string accordingly
// ...

// Update the field with calculated value using the form field number format
formField.TextInputDefault = string.Format(formula, Math.PI);  

@andrew.cousins The value of Calculation form field cannot be set, it is calculated according to the specified expression. In this case number format is applied. Aspose.Words in this case behaves the same way as MS Word does. For example see the following simple code and the documents:

Document doc = new Document(@"C:\Temp\in.docx");

// Set bookmakrs values used for field value calculation.
doc.Range.Bookmarks["NumTest1"].Text = "123.456789";
doc.Range.Bookmarks["NumTest2"].Text = "987.654321";

// Update fields.
doc.UpdateFields();

// Save the output.
doc.Save(@"C:\Temp\out.docx");

in.docx (12.2 KB)
out.docx (9.7 KB)

Thanks for investigating, however MS Word formats this without a space.

Using your example “in.docx” file, I manually edited the Expression to substitute bookmarks NumTest1 and NumTest2 with the numbers you set them to in your code, and then clicked “OK”. The field updated to show negative 864, and there is no space between the first bracket and the “8”, which means MS Word will interpret “#” as no character, where Aspose.Word interprets this as a blank space.
Example.png (12.6 KB)
Example.docx (13.0 KB)

It would seem this may be a bug in Aspose.Word (assuming it is meant to be consistent with MS Word behaviour)?

Is there another character we can use in the formatting that Aspose.Word will interpret the way MS Word does the “#” character?

@andrew.cousins Thank you for additional information. I have managed to reproduce the problem. For a sake of correction it has been logged as WORDSNET-24511. We will further investigate the issue and let you know once it is resolved or we have additional information for you.

Thanks @alexey.noskov. Do you have an idea of timeline to resolve this?

@andrew.cousins The issue is currently in analysis. Once analysis is done we will be able to provide you more information.

1 Like

The issues you have found earlier (filed as WORDSNET-24511) have been fixed in this Aspose.Words for .NET 22.12 update also available on NuGet.