0

I am going to create some thing like a calculator.

There is a TextBox to enter expression and result will be calculated based on entered values and operators. for example users can enter:

(12000+15000)/2

I want to add separator for entered numbers.So the observable expression in TextBox should be :

(12,000+15,000)/2
Almir Vuk
  • 2,983
  • 1
  • 18
  • 22
Behnam
  • 1,039
  • 2
  • 14
  • 39

1 Answers1

3

Use System.Text.RegularExpressions.Regex.Replace() in C#. check it working here msdn docs here

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace Rextester
{
    public class Program
    {
        public static void Main(string[] args)
        {
            string input = "(12000+15000)/2";
            string pattern = "\\d+";
            Regex rgx = new Regex(pattern);
            string result = rgx.Replace(input, callback);

            Console.WriteLine("Original String: {0}", input);
            Console.WriteLine("Replacement String: {0}", result);   
        }

        static string callback(Match m)
        {
            return  string.Format("{0:#,#}", Convert.ToInt32(m.ToString()));
        }
    }
}
Dalton
  • 435
  • 6
  • 12