We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Add comma to digit

@alexey.noskov I am tried to add the comma to the digit I used the below code but it’s removing all space and periods. Kindly help me asap.
Note: I am not modifying or applying the comma to the telephonic number and pin code for example 7890-123654 +1-123-456–7890 +1.123.456.7890 (123) 456–7890 +1 (123) 456–7890-123654 +1-123-456–7890.
Please find the below input and expected output.
Expected_output_numbering.docx (12.9 KB)
Input_numbering.docx (12.9 KB)

FindReplaceOptions options = new FindReplaceOptions();
options.IgnoreDeleted = true;
options.UseSubstitutions = true;

options.ReplacingCallback = new NumberFormatter();

foreach (Paragraph p in ruleBaseModel.SourceDocument.GetChildNodes(NodeType.Paragraph, true))
{
    p.Range.Replace(new Regex(@"(^|\s)(\d{4,})(\s|\.|\,)"), " ", options);
}
public class NumberFormatter : IReplacingCallback
{
    public ReplaceAction Replacing(ReplacingArgs args)
    {
        double number = double.Parse(args.Match.Value.Replace(" ", ""));

        if (number >= 1990 && number <= 2050)
        {
            return ReplaceAction.Skip;
        }
        else
        {
            // Note number formatting depends on the culture.
            CultureInfo info = Thread.CurrentThread.CurrentCulture;
            Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;

            args.Replacement = number.ToString("#,##0.###");

            Thread.CurrentThread.CurrentCulture = info;

            return ReplaceAction.Replace;
        }
    }
}

@Princeshivananjappa You should modify your implementation of IReplacingCallback to take in account the first (^|\s) (whitespaces before the number) and the last group (\s|\.|\,) (whitespaces, coma and period after the number) in the regular expression:

public class NumberFormatter : IReplacingCallback
{
    public ReplaceAction Replacing(ReplacingArgs args)
    {
        double number = double.Parse(args.Match.Groups[2].Value.Replace(" ", ""));

        if (number >= 1990 && number <= 2050)
        {
            return ReplaceAction.Skip;
        }
        else
        {
            // Note number formatting depends on the culture.
            CultureInfo info = Thread.CurrentThread.CurrentCulture;
            Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;

            args.Replacement = args.Match.Groups[1].Value + number.ToString("#,##0.###") + args.Match.Groups[3].Value;

            Thread.CurrentThread.CurrentCulture = info;

            return ReplaceAction.Replace;
        }
    }
}