Перевод дробной части числа из десятичной системы счисления в любую другую систему счисления - C#
Формулировка задачи:
Здравствуйте как перевести дробной части числа из десятичной системы счисления в любую другую систему счисления.
При переводе целых частей выходит все просто
а как сделать с дробными
static void Main(string[] args)
{
string text="5";
string test;
test= Convert.ToString(Convert.ToInt32(text, 10), 2);
Console.WriteLine("{0}", test);
Console.ReadLine();
}Решение задачи: «Перевод дробной части числа из десятичной системы счисления в любую другую систему счисления»
textual
Листинг программы
static void Main(string[] args)
{
//Входная строка
string text;
text1 = Console.ReadLine();
//Преобразование в double
double text1;
text1 = Convert.ToDouble(text);
Console.WriteLine("{0}", text1);
//Воруем целую часть
string zel;
zel = Convert.ToString(Convert.ToInt32(Math.Truncate(text1)), 2);
int zel1 = Convert.ToInt32(Math.Truncate(text1));
Console.WriteLine("{0}", zel, zel1);
//Воруем дробную часть
double text2;
text2 = text1 - Math.Truncate(text1);
Console.WriteLine("{0}", text2);
//Фокус покус это цикл
int cc;
Console.WriteLine("Система счисления");
cc = Convert.ToInt32(Console.ReadLine());
double[] asd = new double[10];
asd[0] = text2;
string drob = null;
for (int i = 1; i < 10; i++)
{
switch (cc)
{
case 2:
asd[i] = (2 * asd[i - 1]) - Math.Truncate(asd[i - 1] * 2);
int bin = Convert.ToInt32(Math.Truncate(asd[i - 1] * 2));
Console.WriteLine("bin={0}", bin);
drob += bin;
break;
case 8:
asd[i] = (8 * asd[i - 1]) - Math.Truncate(asd[i - 1] * 8);
double oct = Math.Truncate(asd[i - 1] * 8);
Console.WriteLine("oct={0}", oct);
drob += oct;
break;
case 16:
asd[i] = (16 * asd[i - 1]) - Math.Truncate(asd[i - 1] * 16);
string hex;
hex = Convert.ToString(Math.Truncate(asd[i - 1] * 16));
int ze;
ze = Convert.ToInt32(hex);
switch (ze)
{
case 10:
hex = "A";
break;
case 11:
hex = "B";
break;
case 12:
hex = "C";
break;
case 13:
hex = "D";
break;
case 14:
hex = "E";
break;
case 15:
hex = "F";
break;
default:
break;
}
Console.WriteLine("hex={0}", hex);
drob += hex;
break;
default:
break;
}
}
Console.WriteLine("{0}.{1}", zel, drob);
Console.ReadLine();
}