A method to convert a positive decimal into a decimal of base 2 9

  • 2020-04-01 23:26:56
  • OfStack

Convert any decimal positive decimal number to 2,3,4,5,6,7,8,9 decimal positive decimal, keeping 8 digits after the decimal point, and output. For example, if the decimal number is 0.795, the output is:

  The decimal positive decimal number 0.795000 is converted into a base 2 number: 0.11001011
  The decimal positive decimal number 0.795000 is converted into a base 3 number: 0.21011011
  The decimal positive decimal number 0.795000 is converted into a base 4 number: 0.30232011
  The decimal positive decimal number 0.795000 is converted into a 5-base number: 0.34414141
  The decimal positive decimal number 0.795000 is converted into a hexadecimal number: 0.44341530
  The decimal positive decimal number 0.795000 is converted into a base 7 number: 0.53645364
  The decimal positive decimal number 0.795000 is converted into an 8-base number: 0.62702436
  The decimal positive decimal number 0.795000 is converted into a base 9 number: 0.71348853
The following code provides this functionality. Where dTestNo represents the decimal decimal to be rotated. IBase stands for base number.


#include <stdio.h>
void fun(double dTestNo, int iBase)
{
 int iT[8];  //The converted decimal retains 8 digits
 int iNo;
 printf(" Decimal positive decimal  %f  Converted to  %d  Hexadecimal number is : ",dTestNo, iBase);
 for(iNo=0;iNo<8;iNo++)  //Work out each number separately & NBSP; The decimal part
 {
  dTestNo *= iBase;
  iT[iNo] = (int)dTestNo;  //So you get the whole number and you store it
  if(dTestNo>=1) dTestNo -= iT[iNo];  //Minus the integer part
 }

 printf("0.");
 for(iNo=0; iNo<8; iNo++) printf("%d", iT[iNo]);
 printf("n");
}
void main ( )
{ 
 double dTestNo= 0.795;
 int iBase;
 for(iBase=2;iBase<=9;iBase++)
  fun(dTestNo,iBase);
 printf("n");
}


Related articles: