Convert any decimal places into 2, 3, 4, 5, 6, 7, 8, and 9 decimal places, retain 8 digits after the decimal point, and output. For example, if the decimal point is 0.795, the output is:
Decimal decimal number: 0.795000
Decimal decimal number: 0.795000
Decimal decimal number: 0.795000
Decimal decimal number: 0.795000
Decimal decimal number: 0.795000
Decimal decimal number: 0.795000
Decimal decimal number: 0.795000
Decimal decimal number: 0.795000
The following code provides this function. DTestNo indicates the decimal number to be converted. IBase indicates the number of hexadecimal digits.
Copy codeThe Code is as follows: # include <stdio. h>
Void fun (double dTestNo, int iBase)
{
Int iT [8]; // The converted decimal places are retained for 8 digits.
Int iNo;
Printf ("Convert decimal positive decimal % f to % d hexadecimal number:", dTestNo, iBase );
For (iNo = 0; iNo <8; iNo ++) // returns the decimal part of each digit.
{
DTestNo * = iBase;
IT [iNo] = (int) dTestNo; // obtain the integer part and store iT.
If (dTestNo> = 1) dTestNo-= iT [iNo]; // subtract the integer
}
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 ");
}