It is necessary for the user to enter a number that will mean the number of characters after the comma of the number PI. I can't do it with the number 4 for some reason. Well, in general, not all numbers in the range from 0-14 work correctly.

static void Main(string[] args) { var numberPi = Math.PI.ToString(); Console.WriteLine("Digits affter dot:"); string userInput = Console.ReadLine(); string newtext4 = Regex.Replace(numberPi.ToString(), @"(\d{"+userInput+"})", string.Empty); Console.WriteLine("The Pi is:{0}",newtext4.ToString()); Console.ReadKey(); } 
  • Because you do not have enough quotes - and the formatted code in the question quite clearly shows this. - Igor
  • Yes, sorry, after the @ sign should be, but this does not solve the problem, unfortunately. - Vlad_Kompanets
  • Why do you need regulars? You know that at the beginning of 2 characters 3. then you can simply take a fixed string of the requested length +2 - Mike
  • I wanted to try practicing with regular ones, and took for example the number of PIs, maybe not very well for a start, but interestingly - Vlad_Kompanets
  • one
    "not all numbers in the range of 0-14 work correctly" - you want to help you? If you have data on which numbers work and which do not, then why dark? - Igor

1 answer 1

The task of displaying the required number of lines after the comma is solved by native means, without regular time:

 Console.WriteLine("Digits affter dot:"); string userInput = Console.ReadLine(); Console.WriteLine(Math.PI.ToString("n" + userInput)); 

nX, where X is a number - this is a standard formatting string for numbers, which allows you to display the required number of decimal places.

If you really want to solve it with a regular schedule, you need to match separately the part that you would like to leave, leaving it when replacing:

 var numberPi = Math.PI.ToString(); Console.WriteLine("Digits affter dot:"); string userInput = Console.ReadLine(); string newtext4 = Regex.Replace(numberPi.ToString(), @"(\d\.\d{" + userInput + @"})\d*", "$1"); Console.WriteLine("The Pi is:{0}", newtext4.ToString()); Console.ReadKey(); 
  • Thank you !!!) this is the option that I wanted to get as a result !!! I will now disassemble that yes how) - Vlad_Kompanets
  • @Vlad_Kompanets is only more careful - not in all cultures. - This is the fraction separator. therefore it is better not to parse the numbers with regulars. - PashaPash
  • @Vlad_Kompanets, moreover, in our cultures the comma is the fraction separator. - ixSci