CSharp · Console
using System;
using System.Globalization;
using System.Numerics;
public static class MyExtensions {
public static T RotateDigitsLeft<T>(this T value, int count) where T : IBinaryInteger<T> {
if (value == T.Zero)
return T.Zero;
string text = value.ToString().TrimStart('-');
count = (count % text.Length + text.Length) % text.Length;
char[] digits = new char[text.Length];
for (int i = 0; i < digits.Length; ++i)
digits[i] = text[(i + count) % digits.Length];
string result = new string(digits).TrimStart('0');
if (value < T.Zero)
result = "-" + result;
return T.Parse(result, CultureInfo.InvariantCulture);
}
public static T RotateDigitsRight<T>(this T value, int count) where T : IBinaryInteger<T> {
if (value == T.Zero)
return T.Zero;
string text = value.ToString().TrimStart('-');
count = text.Length - (count % text.Length + text.Length) % text.Length;
char[] digits = new char[text.Length];
for (int i = 0; i < digits.Length; ++i)
digits[i] = text[(i + count) % digits.Length];
string result = new string(digits);
if (value < T.Zero)
result = "-" + result;
return T.Parse(result, CultureInfo.InvariantCulture);
}
}
public class Program {
public static void Main() {
int value = -12345;
int resultLeft = value.RotateDigitsLeft(1);
int resultRight = value.RotateDigitsRight(1);
Console.WriteLine($"{value} : {resultLeft} : {resultRight}");
}
}